package cn.getech.data.development.service.impl;

import cn.getech.data.development.constant.*;
import cn.getech.data.development.dto.IdAndNameDto;
import cn.getech.data.development.dto.TableAndFieldDto;
import cn.getech.data.development.dto.TableFieldDto;
import cn.getech.data.development.entity.*;
import cn.getech.data.development.entity.dataMap.DataApplication;
import cn.getech.data.development.entity.dataMap.DataApplicationExample;
import cn.getech.data.development.entity.flink.BdpRealTableInfo;
import cn.getech.data.development.entity.permission.*;
import cn.getech.data.development.mapper.*;
import cn.getech.data.development.mapper.dataMap.DataApplicationMapper;
import cn.getech.data.development.mapper.flink.BdpRealTableFieldMapper;
import cn.getech.data.development.mapper.flink.BdpRealTableInfoMapper;
import cn.getech.data.development.mapper.permission.SysRoleDataPermissionMapper;
import cn.getech.data.development.mapper.permission.SysTenantDataPermissionMapper;
import cn.getech.data.development.model.dto.TableAndConlumDto;
import cn.getech.data.development.model.dto.TableInfoAndFieldDto;
import cn.getech.data.development.model.dto.TableSqlDto;
import cn.getech.data.development.model.dto.UserAndTableDto;
import cn.getech.data.development.model.dto.real.RealApproveQueryDTO;
import cn.getech.data.development.model.dto.real.RealApproveReturnDTO;
import cn.getech.data.development.service.*;
import cn.getech.data.development.utils.DataPermissionUtil;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.system.center.entity.RoleRowDataPermissionConfig;
import cn.getech.system.center.entity.SysRoleEntity;
import cn.getech.system.center.entity.SysTenantUser;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.mapper.RoleRowDataPermissionConfigMapper;
import cn.getech.system.center.model.dto.RoleDataPermissionDto;
import cn.getech.system.center.model.dto.RoleRowAndRoleDataPermissionDto;
import cn.getech.system.center.model.dto.RoleRowDataPermissionDto;
import cn.getech.system.center.model.dto.SysUserDto;
import cn.getech.system.center.service.*;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 表信息 服务实现类
 * </p>
 *
 * @author zenith
 * @since 2018-09-18
 */
@Slf4j
@Service
public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableInfo> implements TableInfoService {
    @Resource
    private ProcTableMapper procTableMapper;
    @Autowired
    private AnalysisInfoMapper analysisInfoMapper;
    @Autowired
    private TableInfoMapper tableInfoMapper;
    @Autowired
    private SysTenantDataPermissionMapper sysTenantDataPermissionMapper;
    @Autowired
    private ProcUserMapper procUserMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private IRoleDataPermissionConfigService iRoleDataPermissionConfigService;
    @Autowired
    private RoleRowDataPermissionConfigMapper roleRowDataPermissionConfigMapper;
    @Autowired
    private DataPermissionParamMapper dataPermissionParamMapper;
    @Autowired
    private TableFieldInfoMapper tableFieldInfoMapper;
    @Autowired
    private AllPermissionConfigMapper allPermissionConfigMapper;
    @Autowired
    private TableFieldRulesMapper tableFieldRulesMapper;
    @Autowired
    private ProcInfoMapper procInfoMapper;
    @Autowired
    private TableRelationMapper tableRelationMapper;
    @Autowired
    private TableFieldMaskRulesMapper tableFieldMaskRulesMapper;
    @Autowired
    private RangerDataService rangerDataService;

    @Autowired
    private SysTenantUserService sysTenantUserService;

    @Autowired
    private SysRoleDataPermissionMapper sysRoleDataPermissionMapper;

    @Autowired
    private DataPermissionService dataPermissionService;

    @Autowired
    private BdpRealTableInfoMapper bdpRealTableInfoMapper;

    @Autowired
    private BdpRealTableFieldMapper realTableFieldMapper;

    @Autowired
    private IRoleRowDataPermissionConfigService iRoleRowDataPermissionConfigService;
    @Autowired
    private ITableFieldRulesService iTableFieldRulesService;
    @Autowired
    private ITableFieldMaskRulesService iTableFieldMaskRulesService;

    @Autowired
    private TableRelationshipService tableRelationshipService;

    @Autowired
    private TableFieldInfoService tableFieldInfoService;

    @Autowired
    private TableRelationService tableRelationService;

    @Autowired
    private TableRegisterService tableRegisterService;

    @Override
    public void saveModelRelation(TableInfo tableInfo) {
//        if (CollectionUtils.isNotEmpty(tableInfo.getSubjectIds())
//                && tableInfo.getId() != null && tableInfo.getUpdatePer() != null) {
//            tableInfoRelationMapper.deleteSubjectRelation(tableInfo.getId(), null, StoreTableType.OFFLINE.getCode());
//            for (Integer subjectId : tableInfo.getSubjectIds()) {
//                tableInfoRelationMapper.addSubjectRelation(tableInfo.getId(), subjectId, tableInfo.getUpdatePer(), StoreTableType.OFFLINE.getCode());
//            }
//        }
//        if (CollectionUtils.isNotEmpty(tableInfo.getCategoryIds())
//                && tableInfo.getId() != null && tableInfo.getUpdatePer() != null) {
//            tableInfoRelationMapper.deleteCategoryRelation(tableInfo.getId(), null, StoreTableType.OFFLINE.getCode());
//            for (Integer categoryId : tableInfo.getCategoryIds()) {
//                tableInfoRelationMapper.addCategoryRelation(tableInfo.getId(), categoryId, tableInfo.getUpdatePer(), StoreTableType.OFFLINE.getCode());
//            }
//        }
        if(tableInfo.getId()!=null){
            //离线表旧有目录关系不插入，换用新的插入方式
            if(tableInfo.getCateGoryIdsList()!=null){
                //先删除旧有关系
                this.baseMapper.deleteCategory(tableInfo.getId());
                for(String categoryId:tableInfo.getCateGoryIdsList()){
                    Map<String,Object> map = new HashMap<>();
                    map.put("categoryId",Integer.valueOf(categoryId));
                    map.put("tableId",tableInfo.getId());
                    map.put("tenantId",ShiroUtils.getTenantId());
                    map.put("userId",ShiroUtils.getUserId().intValue());
                    map.put("datetime",new Date());
                    this.baseMapper.insertRelation(map);
                }
            }
        }
    }


    /**
     * 判断该表是否为用户所创建
     */
    @Override
    public boolean tableOwner(Long uid, Integer tid) {
        QueryWrapper<TableInfo> queryWrapper = new QueryWrapper<>();
        return baseMapper.selectCount(queryWrapper.eq("id", tid).eq("create_per", uid)) > 0;
    }

    @Override
    public JSONArray listDbsAndTables(Integer procId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("noNeedTableField", 1);
        stringObjectHashMap.put("procId", procId);
        return roleAndPermission(stringObjectHashMap, ShiroUtils.getUserId());
    }

    @Override
    public void dropTables(String tableIds) {
        if (StringUtils.isNotEmpty(tableIds)) {
            String[] split = tableIds.split(",");
            if (null != split && split.length > 0) {
                for (String s : split) {
                    TableInfo tableInfo = tableInfoMapper.selectById(s);
                    if (null != tableInfo) {
                        String dbName = tableInfo.getDbName();
                        String tableName = tableInfo.getTableName();
                        //删除仓库表
                        dropTable(s);

                        //删除字段
                        deleteAllTableFileds(s);

                        //删除与这个关联的血缘关系表
                        deleteAllRelation(s);

                        //删除关联的项目关联
                        deleteProRel(s);

                        //删除对应的脱敏规则
                        deleteMaskingRule(s);

                        //删除ranger策略
                        rangerDataService.deleteRangerPloicyByTableId(tableInfo.getId());
                    }
                }
            }
        }
    }

    private void deleteAllRelation(String s) {
        tableRelationMapper.delete(new QueryWrapper<TableRelation>().eq("cid", s).or().eq("pid", s));
    }

    private void deleteAllTableFileds(String s) {
        tableFieldInfoMapper.delete(new QueryWrapper<TableFieldInfo>().eq("table_id", s));
    }

    private void deleteMaskingRule(String s) {
        tableFieldRulesMapper.delete(new QueryWrapper<TableFieldRules>().eq("table_id", s));
        roleRowDataPermissionConfigMapper.delete(new QueryWrapper<RoleRowDataPermissionConfig>().eq("table_id", s));
        tableFieldMaskRulesMapper.delete(new QueryWrapper<TableFieldMaskRules>().eq("table_id", s));
    }

    private void deleteProRel(String s) {
        procTableMapper.delete(new QueryWrapper<ProcTable>().eq("table_id", s));
    }

    private void dropTable(String s) {
        tableInfoMapper.deleteById(s);
    }

    /**
     * 判断是否包含在项目中
     *
     * @param id
     * @return
     */
    @Override
    public boolean checkIfRef(Integer id) {
        return this.procTableMapper.selectCount(new QueryWrapper<ProcTable>().eq("table_id", id)) > 0;
    }

    @Override
    public boolean checkExistByName(String dbName, String table_name) {
        return this.baseMapper.selectCount(new QueryWrapper<TableInfo>().eq("db_name", dbName).eq("table_name", table_name)) > 0;
    }

    @Override
    public boolean checkPermission(Integer id) {
        TableInfo entity = this.baseMapper.selectById(id);
        return entity.getCreatePer() == ShiroUtils.getUserId().intValue();
    }

    @Override
    public boolean checkExistById(Integer id) {
        return this.baseMapper.selectById(id) != null;
    }

    @Autowired
    private SysUserService sysUserService;

    /**
     * 我的授权列表 （实时表）
     *
     * @param realApproveQueryDTO
     */
    @Override
    public IPage<RealApproveReturnDTO> realApproveList(Map<String, Object> params, RealApproveQueryDTO realApproveQueryDTO) {
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        IPage<RealApproveReturnDTO> page = new Page<>(realApproveQueryDTO.getCurrent(), realApproveQueryDTO.getSize());
        page = baseMapper.realApproveList(page, realApproveQueryDTO, params);
        if (CollectionUtil.isEmpty(page.getRecords())) {
            return page;
        }
        List<Integer> tableIds = page.getRecords().stream().map(RealApproveReturnDTO::getId).collect(Collectors.toList());
        Map<Integer, String> projectNamesMap = Maps.newHashMap();
        List<TableInfo> projectNames = baseMapper.getRealProjectByTableIds(tableIds);
        if (CollectionUtil.isNotEmpty(projectNames)) {
            for (TableInfo projectName : projectNames) {
                projectNamesMap.put(projectName.getId(), projectName.getProjectName());
            }
        }
        for (RealApproveReturnDTO record : page.getRecords()) {
            //添加创建用户的信息
            if (record.getCreateUser() != null) {
                Long userId = Long.valueOf(record.getCreateUser());
                record.setCreatePerName(userMap.get(userId));
            }
            if (record.getApplyUserId() != null) {
                record.setApplyUser(userMap.get(Long.valueOf(record.getApplyUserId())));
            }
            if (record.getApproveUserId() != null) {
                record.setApproveUser(userMap.get(Long.valueOf(record.getApproveUserId())));
            }
            record.setProjectName(projectNamesMap.get(record.getId()));
        }
        return page;
    }


    @Override
    public Boolean deleteDbTable(TableInfo param, List<TableRegisterEntity> registerEntityList, boolean falge) {
        Boolean b = removeById(param.getId());
        if (b) {
            Map<String, Object> query = new HashMap<>();
            query.put("table_id", param.getId());
            tableFieldInfoService.removeByMap(query);

            Integer registerType = falge ? TableRegisterTypeEnum.OFFLINE_DELETE.getCode() : TableRegisterTypeEnum.OFFLINE_UPDATE.getCode();
            if (falge) {
                Map<String, Object> relationQuery = new HashMap<>();
                relationQuery.put("source_table_id", param.getId());
                tableRelationshipService.removeByMap(relationQuery);

                //删除数据仓库的同时删除与之相关联的血缘关系
                Map<String, Object> queryRe = new HashMap<>();
                queryRe.put("cid", param.getId());
                tableRelationService.removeByMap(queryRe);
                queryRe.clear();
                queryRe.put("pid", param.getId());
                tableRelationService.removeByMap(queryRe);

                //删除对应的表的规则和 角色和规则的关联表的数据 和对应的脱敏规则信息
                iTableFieldRulesService.remove(new QueryWrapper<TableFieldRules>().eq("table_id", param.getId()));
                iRoleRowDataPermissionConfigService.remove(new QueryWrapper<RoleRowDataPermissionConfig>().eq("table_id", param.getId()));
                iTableFieldMaskRulesService.remove(new QueryWrapper<TableFieldMaskRules>().eq("table_id", param.getId()));
            }

            TableRegisterEntity registerEntity = new TableRegisterEntity();
            registerEntity.setTableInfoId(param.getId());
            registerEntity.setRegisterType(registerType);
            registerEntity.setRunUser(ShiroUtils.getUserId());
            registerEntityList.add(registerEntity);
        }
        return b;
    }


    @Override
    public void addRegisterDelete(long startTime, List<TableRegisterEntity> registerEntityList, Integer importChangeRole) {
        if (CollectionUtil.isNotEmpty(registerEntityList)) {
            Date curr = new Date();
            long cost = curr.getTime() - startTime;
            Long roleId = null == importChangeRole ? 1L : Long.valueOf(importChangeRole);
            registerEntityList.stream().forEach(r -> {
                r.setCost(cost);
                r.setRunTime(curr);
                r.setBatchNum(curr.getTime());
                r.setRoleId(roleId);
            });

            tableRegisterService.saveBatch(registerEntityList);
        }
    }

    /**
     * 根据关联的血缘表查询相关的表id
     *
     * @param relationTableName
     */
    @Override
    public List<Long> selectTableIdByTableName(String relationTableName) {
        return tableInfoMapper.selectTableIdByTableName(relationTableName);
    }

    /**
     * 删除相关的质量报告信息
     *
     * @param dbName
     * @param tableName
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDataQualityInfo(String dbName, String tableName) {
        Integer qualityInfoId = tableInfoMapper.selectQualityInfo(dbName, tableName);
        if (Objects.isNull(qualityInfoId)) {
            return;
        }
        tableInfoMapper.delMonitorInfo(qualityInfoId);
        tableInfoMapper.delMonitorRule(qualityInfoId);
        tableInfoMapper.delMonitorRuleHistory(qualityInfoId);
        tableInfoMapper.delQualityDaily(qualityInfoId);
        tableInfoMapper.delQualityExcelDaily(qualityInfoId);
        tableInfoMapper.delQualityInfo(qualityInfoId);
        tableInfoMapper.delQualityRule(qualityInfoId);
        tableInfoMapper.delQualityRuleDaily(qualityInfoId);
        tableInfoMapper.delQualitySubscribeInfo(qualityInfoId);
        tableInfoMapper.delQualitySubscribeObject(qualityInfoId);
    }

    @Override
    public Boolean initKuduChange(List<TableFieldInfo> updateTableFields, List<TableFieldInfo> dropTableFields,
                                  List<TableFieldInfo> addTableFields, List<TableFieldInfo> updateTablePros,
                                  List<TableFieldInfo> tableFieldInfos, List<TableFieldInfo> olderTbaleFields) {
        //组装对应的类型
        if (CollectionUtil.isEmpty(tableFieldInfos)) {
            if (CollectionUtil.isEmpty(olderTbaleFields)) {
                return false;
            } else {
                dropTableFields.addAll(olderTbaleFields);
                return true;
            }
        }
        if (CollectionUtil.isEmpty(olderTbaleFields)) {
            addTableFields.addAll(tableFieldInfos);
            return true;
        }
        Boolean flage = false;
        for (TableFieldInfo tableFieldInfo : tableFieldInfos) {
            TableFieldInfo tableFieldInfo1 = olderTbaleFields.stream().filter(o -> Objects.equals(o.getFieldName(), tableFieldInfo.getFieldName())).findAny().orElse(null);
            if (null == tableFieldInfo1) {
                addTableFields.add(tableFieldInfo);
                flage = true;
            } else {
                if (!isKuduTableFieldType(tableFieldInfo1.getFieldType(), tableFieldInfo.getFieldType())) {
                    TableFieldInfo newTableFieldInfo = new TableFieldInfo();
                    BeanUtils.copyProperties(tableFieldInfo, newTableFieldInfo);
                    newTableFieldInfo.setOlderFieldName(tableFieldInfo1.getFieldName());
                    newTableFieldInfo.setOlderFieldType(tableFieldInfo1.getFieldType());
                    updateTablePros.add(newTableFieldInfo);
                    flage = true;
                }
            }
        }
        for (TableFieldInfo tableFieldInfo : olderTbaleFields) {
            TableFieldInfo tableFieldInfo1 = tableFieldInfos.stream().filter(o -> Objects.equals(o.getFieldName(), tableFieldInfo.getFieldName())).findAny().orElse(null);
            if (null == tableFieldInfo1) {
                dropTableFields.add(tableFieldInfo);
                flage = true;
            }
        }
        return flage;
    }

    @Override
    public Boolean initKuduChangeSql(List<TableFieldInfo> updateTableFields, List<TableFieldInfo> dropTableFields,
                                     List<TableFieldInfo> addTableFields, List<TableFieldInfo> updateTablePros,
                                     List<TableFieldInfo> tableFieldInfos, List<TableFieldInfo> olderTbaleFields) {
        //如果有id 则为修改  无id则为新增
        if (CollectionUtil.isEmpty(tableFieldInfos)) {
            if (CollectionUtil.isEmpty(olderTbaleFields)) {
                return false;
            } else {
                dropTableFields.addAll(olderTbaleFields);
                return true;
            }
        }
        if (CollectionUtil.isEmpty(olderTbaleFields)) {
            addTableFields.addAll(tableFieldInfos);
            return true;
        }
        Boolean flage = false;
        //全部新增的
        List<TableFieldInfo> tableFieldInfos1 = tableFieldInfos.stream().filter(o -> Objects.equals(o.getId(), null)).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(tableFieldInfos1)) {
            //全部是新增的
            addTableFields.addAll(tableFieldInfos1);
            flage = true;
        }
        for (TableFieldInfo tableFieldInfo : olderTbaleFields) {
            TableFieldInfo tableFieldInfo1 = tableFieldInfos.stream().filter(o -> Objects.equals(o.getId(), tableFieldInfo.getId())).findAny().orElse(null);
            if (null == tableFieldInfo1) {
                dropTableFields.add(tableFieldInfo);
                flage = true;
            } else {
                if (Objects.equals(tableFieldInfo.getFieldName(), tableFieldInfo1.getFieldName())) {
                    //kudu表的字段类型是否修改的问题（有些字段需要转换）
                    if (isKuduTableFieldType(tableFieldInfo.getFieldType(), tableFieldInfo1.getFieldType())) {
                        //全部相同，字段不变
                    } else {
                        updateTablePros.add(tableFieldInfo1);
                        flage = true;
                    }
                } else {
                    //负责改字段名字
                    TableFieldInfo newTableFieldInfo = new TableFieldInfo();
                    BeanUtils.copyProperties(tableFieldInfo, newTableFieldInfo);
                    newTableFieldInfo.setOlderFieldName(tableFieldInfo.getFieldName());
                    newTableFieldInfo.setOlderFieldType(tableFieldInfo1.getFieldType());
                    newTableFieldInfo.setFieldName(tableFieldInfo1.getFieldName());
                    newTableFieldInfo.setFieldType(tableFieldInfo1.getFieldType());
                    updateTableFields.add(newTableFieldInfo);
                    flage = true;
                    if (!isKuduTableFieldType(tableFieldInfo.getFieldType(), tableFieldInfo1.getFieldType())) {
                        //负责该字段属性
                        TableFieldInfo newTableFieldInfo1 = new TableFieldInfo();
                        BeanUtils.copyProperties(tableFieldInfo1, newTableFieldInfo1);
                        newTableFieldInfo1.setOlderFieldName(tableFieldInfo.getFieldName());
                        newTableFieldInfo1.setOlderFieldType(tableFieldInfo.getFieldType());
                        newTableFieldInfo1.setFieldName(tableFieldInfo.getFieldName());
                        newTableFieldInfo1.setFieldType(tableFieldInfo1.getFieldType());
                        updateTablePros.add(newTableFieldInfo1);
                    }
                }
            }
        }
        return flage;
    }

    private boolean isKuduTableFieldType(String fieldType, String fieldType1) {
        //判断kudu表的字段类型是否一样
        return Objects.equals(fieldType, fieldType1);
    }

    @Override
    public List<TableInfoAndFieldDto> initKuduSql(List<TableFieldInfo> updateTableFields, List<TableFieldInfo> dropTableFields,
                                                  List<TableFieldInfo> addTableFields, List<TableFieldInfo> updateTablePros,
                                                  TableInfo tableInfo) {
        //初始化sql语句
        //先判断有没有修改字段属性的
        List<TableInfoAndFieldDto> rests = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(updateTablePros)) {
            for (TableFieldInfo tableFieldInfo : updateTablePros) {
                TableInfoAndFieldDto tableInfoAndFieldDto = kuduUpdateTablePros(tableFieldInfo, tableInfo);
                if (null != tableInfoAndFieldDto) {
                    rests.add(tableInfoAndFieldDto);
                }
            }
        }
        if (CollectionUtil.isNotEmpty(updateTableFields)) {
            for (TableFieldInfo tableFieldInfo : updateTableFields) {
                TableInfoAndFieldDto tableInfoAndFieldDto = kuduUpdateTable(tableFieldInfo, tableInfo);
                if (null != tableInfoAndFieldDto) {
                    rests.add(tableInfoAndFieldDto);
                }
            }
        }
        if (CollectionUtil.isNotEmpty(dropTableFields)) {
            for (TableFieldInfo tableFieldInfo : dropTableFields) {
                TableInfoAndFieldDto tableInfoAndFieldDto = kuduDropTable(tableFieldInfo, tableInfo);
                if (null != tableInfoAndFieldDto) {
                    rests.add(tableInfoAndFieldDto);
                }
            }
        }
        if (CollectionUtil.isNotEmpty(addTableFields)) {
            for (TableFieldInfo tableFieldInfo : addTableFields) {
                TableInfoAndFieldDto tableInfoAndFieldDto = kuduAddTable(tableFieldInfo, tableInfo);
                if (null != tableInfoAndFieldDto) {
                    rests.add(tableInfoAndFieldDto);
                }
            }
        }
        return rests;
    }

    private TableInfoAndFieldDto kuduAddTable(TableFieldInfo tableFieldInfo, TableInfo tableInfo) {
        //组装头部
        TableInfoAndFieldDto tableInfoAndFieldDto = heardData(tableInfo);
        //数据
        TableSqlDto tableSqlDto = new TableSqlDto();
        tableSqlDto.setTableId(tableInfo.getId());
        tableSqlDto.setDbName(tableInfo.getDbName());
        tableSqlDto.setTableName(tableInfo.getTableName());
        tableSqlDto.setOlderTableFieldName(tableFieldInfo.getFieldName());
        tableSqlDto.setOlderFieldType(tableFieldInfo.getFieldType());
        tableSqlDto.setNewTableFieldName(tableFieldInfo.getFieldName());
        tableSqlDto.setNewFieldType(tableFieldInfo.getFieldType());
        tableSqlDto.setSql(addSql(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getFieldName(), tableFieldInfo.getFieldType()));
        tableSqlDto.setIsExces(0);
        tableSqlDto.setIsChangeProsType(0);//新增
        tableInfoAndFieldDto.setTableSqlDtos(Arrays.asList(tableSqlDto));
        return tableInfoAndFieldDto;
    }

    private TableInfoAndFieldDto heardData(TableInfo tableInfo) {
        TableInfoAndFieldDto tableInfoAndFieldDto = new TableInfoAndFieldDto();
        tableInfoAndFieldDto.setId(tableInfo.getId());
        tableInfoAndFieldDto.setDbName(tableInfo.getDbName());
        tableInfoAndFieldDto.setTableName(tableInfo.getTableName());
        return tableInfoAndFieldDto;
    }

    private String addSql(String dbName, String tableName, String fieldName, String fieldType) {
        return "ALTER TABLE " + dbName + "." + tableName + " ADD COLUMN " + fieldName + " " + fieldType;
    }

    private TableInfoAndFieldDto kuduDropTable(TableFieldInfo tableFieldInfo, TableInfo tableInfo) {
        //组装头部
        TableInfoAndFieldDto tableInfoAndFieldDto = heardData(tableInfo);
        //数据
        TableSqlDto tableSqlDto = new TableSqlDto();
        tableSqlDto.setTableId(tableInfo.getId());
        tableSqlDto.setDbName(tableInfo.getDbName());
        tableSqlDto.setTableName(tableInfo.getTableName());
        tableSqlDto.setOlderTableFieldName(tableFieldInfo.getFieldName());
        tableSqlDto.setOlderFieldType(tableFieldInfo.getFieldType());
        tableSqlDto.setNewTableFieldName(tableFieldInfo.getFieldName());
        tableSqlDto.setNewFieldType(tableFieldInfo.getFieldType());
        tableSqlDto.setSql(dropSql(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getFieldName()));
        tableSqlDto.setIsExces(0);
        tableSqlDto.setIsChangeProsType(1);//删除
        tableInfoAndFieldDto.setTableSqlDtos(Arrays.asList(tableSqlDto));
        return tableInfoAndFieldDto;
    }

    private String dropSql(String dbName, String tableName, String fieldName) {
        return "ALTER TABLE " + dbName + "." + tableName + " DROP " + fieldName;
    }

    private TableInfoAndFieldDto kuduUpdateTable(TableFieldInfo tableFieldInfo, TableInfo tableInfo) {
        //组装头部
        TableInfoAndFieldDto tableInfoAndFieldDto = heardData(tableInfo);
        //数据
        TableSqlDto tableSqlDto = new TableSqlDto();
        tableSqlDto.setTableId(tableInfo.getId());
        tableSqlDto.setDbName(tableInfo.getDbName());
        tableSqlDto.setTableName(tableInfo.getTableName());
        //这个字段很重要---但是转过来可能没有修改的字段，会算到新增的字段
        tableSqlDto.setOlderTableFieldName(tableFieldInfo.getOlderFieldName());
        tableSqlDto.setOlderFieldType(tableFieldInfo.getFieldType());
        tableSqlDto.setNewTableFieldName(tableFieldInfo.getFieldName());
        tableSqlDto.setNewFieldType(tableFieldInfo.getFieldType());
        tableSqlDto.setSql(updateSql(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getOlderFieldName(), tableFieldInfo.getFieldName(), tableFieldInfo.getFieldType()));
        tableSqlDto.setIsExces(0);
        tableSqlDto.setIsChangeProsType(2);//修改字段名字
        tableInfoAndFieldDto.setTableSqlDtos(Arrays.asList(tableSqlDto));
        return tableInfoAndFieldDto;
    }

    private String updateSql(String dbName, String tableName, String olderFieldName, String newFieldName, String fieldType) {
        return "ALTER TABLE " + dbName + "." + tableName + " CHANGE  " + olderFieldName + " " + newFieldName + " " + fieldType;
    }

    private TableInfoAndFieldDto kuduUpdateTablePros(TableFieldInfo tableFieldInfo, TableInfo tableInfo) {
        //组装头部
        TableInfoAndFieldDto tableInfoAndFieldDto = heardData(tableInfo);
        List<TableSqlDto> sqls = new ArrayList<>();
        //修改字段属性
        TableSqlDto tableSqlDto1 = updateSqlProsTableDtoData(tableInfo, tableFieldInfo, 0,
                addSql(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getFieldName() + "_copy_tmp_gctcl", tableFieldInfo.getFieldType()));
        sqls.add(tableSqlDto1);

        TableSqlDto tableSqlDto2 = updateSqlProsTableDtoData(tableInfo, tableFieldInfo, 1,
                updateSqlPros(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getFieldName() + "_copy_tmp_gctcl", tableFieldInfo.getOlderFieldName(), tableFieldInfo.getFieldType()));
        sqls.add(tableSqlDto2);

        TableSqlDto tableSqlDto3 = updateSqlProsTableDtoData(tableInfo, tableFieldInfo, 2,
                dropSql(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getOlderFieldName()));
        sqls.add(tableSqlDto3);


        TableSqlDto tableSqlDto4 = updateSqlProsTableDtoData(tableInfo, tableFieldInfo, 3,
                updateSql(tableInfo.getDbName(), tableInfo.getTableName(), tableFieldInfo.getFieldName() + "_copy_tmp_gctcl", tableFieldInfo.getFieldName(), tableFieldInfo.getFieldType()));
        sqls.add(tableSqlDto4);

        tableInfoAndFieldDto.setTableSqlDtos(sqls);
        return tableInfoAndFieldDto;
    }

    private TableSqlDto updateSqlProsTableDtoData(TableInfo tableInfo, TableFieldInfo tableFieldInfo, Integer step, String sql) {
        TableSqlDto tableSqlDto1 = new TableSqlDto();
        tableSqlDto1.setTableId(tableInfo.getId());
        tableSqlDto1.setDbName(tableInfo.getDbName());
        tableSqlDto1.setTableName(tableInfo.getTableName());
        tableSqlDto1.setOlderTableFieldName(tableFieldInfo.getOlderFieldName());
        tableSqlDto1.setOlderFieldType(tableFieldInfo.getOlderFieldType());
        tableSqlDto1.setNewTableFieldName(tableFieldInfo.getFieldName());
        tableSqlDto1.setNewFieldType(tableFieldInfo.getFieldType());
        tableSqlDto1.setSql(sql);
        tableSqlDto1.setIsExces(0);
        tableSqlDto1.setIsChangeProsType(3);//修改字段属性
        tableSqlDto1.setIsSteps(step);
        return tableSqlDto1;
    }

    private String updateSqlPros(String dbName, String tableName, String fieldName, String olderFieldName, String fieldType) {
        return "UPDATE " + dbName + "." + tableName + " SET " + fieldName + "=cast(" + olderFieldName + " as " + fieldType + ")";
    }

    @Override
    public String rockKuduSql(TableSqlDto tableSqlDto) {
        String sql = "";
        if (tableSqlDto != null) {
            switch (tableSqlDto.getIsChangeProsType()) {
                //老的是新增
                case 0:
                    sql = dropSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getNewTableFieldName());
                    break;
                //老的是删除
                case 1:
                    sql = addSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getNewTableFieldName(), tableSqlDto.getNewFieldType());
                    break;
                case 2:
                    //修改字段名
                    sql = updateSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getNewTableFieldName(), tableSqlDto.getOlderTableFieldName(), tableSqlDto.getOlderFieldType());
                    break;
                case 3:
                    //不会是这种类型，出现在这个方法中
                    //修改字段属性
                    break;
            }
        }
        return sql;
    }

    @Override
    public List<TableSqlDto> rockSteps(List<TableSqlDto> tmpTableSqlDtos, Integer isSteps) {
        List<TableSqlDto> tableSqlDtos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(tmpTableSqlDtos)) {
            //第多少步 0:新增一个字段 xxxx_copy_tmp_gctcl  1:将数据赋值给这个字段  2:删除之前的字段 3:修改字段名字 xxxx  4:用于全部回滚
            TableSqlDto tableSqlDto = tmpTableSqlDtos.get(0);
            switch (isSteps) {
                case 0:
                    //老的是新增一个字段
                    //不需要
                    break;
                case 1:
                    //老的是赋值给这个新的字段
                case 2:
                    //删除之前的字段
                    String sql = dropSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName() + "_copy_tmp_gctcl");
                    TableSqlDto newTableSqlDto = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto);
                    newTableSqlDto.setSql(sql);
                    tableSqlDtos.add(newTableSqlDto);
                    break;
                case 3:
                    //修改字段名字 xxxx
                    String sql3 = dropSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName() + "_copy_tmp_gctcl");
                    TableSqlDto newTableSqlDto3 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto3);
                    newTableSqlDto3.setSql(sql3);
                    tableSqlDtos.add(newTableSqlDto3);

                    //将 老字段_copy_tmp_gctcl 赋值给原来的老字段
                    String sql2 = updateSqlPros(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName(), tableSqlDto.getOlderTableFieldName() + "_copy_tmp_gctcl", tableSqlDto.getOlderFieldType());
                    TableSqlDto newTableSqlDto2 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto2);
                    newTableSqlDto2.setSql(sql2);
                    tableSqlDtos.add(newTableSqlDto2);

                    //新增老的字段
                    String sql1 = addSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName(), tableSqlDto.getOlderFieldType());
                    TableSqlDto newTableSqlDto1 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto1);
                    newTableSqlDto1.setSql(sql1);
                    tableSqlDtos.add(newTableSqlDto1);

                    break;
                case 4:
                    //用于全部回滚 **** 从下往上执行
                    //修改新字段的名字
                    String sql41 = updateSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName() + "_copy_tmp_gctcl", tableSqlDto.getOlderTableFieldName(), tableSqlDto.getOlderFieldType());
                    TableSqlDto newTableSqlDto41 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto41);
                    newTableSqlDto41.setSql(sql41);
                    tableSqlDtos.add(newTableSqlDto41);


                    //删除老的字段
                    String sql42 = dropSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName());
                    TableSqlDto newTableSqlDto42 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto42);
                    newTableSqlDto42.setSql(sql42);
                    tableSqlDtos.add(newTableSqlDto42);

                    //赋值给新增字段
                    String sql43 = updateSqlPros(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName() + "_copy_tmp_gctcl", tableSqlDto.getOlderTableFieldName(), tableSqlDto.getOlderFieldType());
                    TableSqlDto newTableSqlDto43 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto43);
                    newTableSqlDto43.setSql(sql43);
                    tableSqlDtos.add(newTableSqlDto43);

                    //新增中间字段
                    String sql44 = addSql(tableSqlDto.getDbName(), tableSqlDto.getTableName(), tableSqlDto.getOlderTableFieldName() + "_copy_tmp_gctcl", tableSqlDto.getOlderFieldType());
                    TableSqlDto newTableSqlDto44 = new TableSqlDto();
                    BeanUtils.copyProperties(tableSqlDto, newTableSqlDto44);
                    newTableSqlDto44.setSql(sql44);
                    tableSqlDtos.add(newTableSqlDto44);

                    break;
            }
        }
        return tableSqlDtos;
    }

    @Override
    public PageUtils selectListOwnPage(Map<String, Object> params) {
        //如果前端没传创建人ID,则设置创建人ID为NULL，防止报错
        if (params.get("createUserId") == null) {
            params.put("createUserId", null);
        }
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        IPage<TableInfo> page = this.baseMapper.selectListOwnPage((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        Map<Integer, String> projectNamesMap = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            List<Integer> ids = page.getRecords().stream().map(TableInfo::getId).collect(Collectors.toList());
            List<TableInfo> projectNames = baseMapper.getTableProjectNamesByTableId(ids);
            if (CollectionUtil.isNotEmpty(projectNames)) {
                for (TableInfo projectName : projectNames) {
                    projectNamesMap.put(projectName.getId(), projectName.getProjectName());
                }
            }
        }
        for (TableInfo tableInfo : page.getRecords()) {
            //添加创建用户的信息
            if (tableInfo.getCreatePer() != null) {
                Long userId = Long.valueOf(tableInfo.getCreatePer());
                tableInfo.setCreatePerName(userMap.get(userId));
            }
            if (tableInfo.getApplyUserId() != null) {
                tableInfo.setApplyUser(userMap.get(tableInfo.getApplyUserId()));
            }
            if (tableInfo.getApproveUserId() != null) {
                tableInfo.setApproveUser(userMap.get(tableInfo.getApproveUserId()));
            }
            tableInfo.setProjectName(projectNamesMap.get(tableInfo.getId()));
        }
        return new PageUtils(page);
    }

    @Autowired
    private DataApplicationMapper dataApplicationMapper;

    @Override
    public TableInfo getDetailById(Integer id) {
        Long userId = ShiroUtils.getUserId();
        Integer tenantId = ShiroUtils.getTenantId();
        TableInfo t = baseMapper.getTableById(id);
        //平台管理员是没有租户的  tenantId==null
        cheackTables(id, userId.intValue(), tenantId);
        List<TableInfo> projectNames = baseMapper.getTableProjectNamesByTableId(Collections.singletonList(id));
        if (CollectionUtil.isNotEmpty(projectNames)) {
            t.setProjectName(projectNames.get(0).getProjectName());
        }
        DataApplicationExample param = new DataApplicationExample();
        param.createCriteria().andTableIdEqualTo(id)
                .andUserIdEqualTo(userId);
        param.setOrderByClause("id desc");
        List<DataApplication> list = dataApplicationMapper.selectByExample(param);
        if (CollectionUtils.isNotEmpty(list)) {
            t.setApplyStatus(list.get(0).getApplyStatus());
        } else {
            t.setApplyStatus(ApplyStatusEnum.NONE.getType());
        }
        Set<Integer> subjectIds = tableRelationMapper.selectSubjectIds(id, StoreTableType.OFFLINE.getCode());
        if (CollectionUtils.isNotEmpty(subjectIds)) {
            t.setSubjectIds(subjectIds);
        } else {
            t.setSubjectIds(Sets.newHashSet());
        }
        Set<Integer> categoryIds = tableRelationMapper.selectCategoryIds(id, StoreTableType.OFFLINE.getCode());
        if (CollectionUtils.isNotEmpty(categoryIds)) {
            t.setCategoryIds(categoryIds);
        } else {
            t.setCategoryIds(Sets.newHashSet());
        }
        return t;
    }

    @Override
    public void cheackTables(Integer id, Integer userId, Integer tenantId) {
        //不是平台管理员的权限验证
        if (null != tenantId) {
            List<Integer> dataIds = DataPermissionUtil.getDataIds(userId.intValue(), PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
            //如果是普通用户，还得添加上直接创建的ids
            if (!DataPermissionUtil.isAdmin(userId)) {
                //租户管理员的表
                List<TableInfo> tableInfos = tableInfoMapper.selectList(new LambdaQueryWrapper<TableInfo>().eq(TableInfo::getCreatePer, userId).eq(TableInfo::getTenantId, tenantId));
                if (CollectionUtil.isNotEmpty(tableInfos)) {
                    Set<Integer> ids = tableInfos.stream().map(TableInfo::getId).collect(Collectors.toSet());
                    if (dataIds == null) {
                        dataIds = new ArrayList<>();
                    }
                    dataIds.addAll(ids);
                }
            }
            if (CollectionUtil.isEmpty(dataIds)) {
                throw new RRException("该用户无权查看当前表的信息！");
            }
            if (!dataIds.contains(id)) {
                throw new RRException("该用户无权查看当前表的信息！");
            }
        }
    }

    @Override
    public JSONObject selectApplyListPage(Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        Integer tenantId = ShiroUtils.getTenantId();
        params.put("tenantId", tenantId);
        params.put("userId", userId);
        JSONObject data = new JSONObject();
        //离线
        if (params.get("flag") != null && params.get("flag").equals(1)) {
            IPage<TableInfo> page = this.baseMapper.selectApplyListPage((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
            List<Integer> ids = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(page.getRecords())) {
                for (TableInfo record : page.getRecords()) {
                    if (StringUtils.isNotEmpty(record.getProjectName())) {
                        String s = record.getProjectName().replaceAll(",", "、");
                        record.setProjectName(s);
                    }
                    if (record.getApplyStatus() == null) {
                        record.setApplyStatus(ApplyStatusEnum.NONE.getType());
                    }
                    if (ApplyStatusEnum.WAIT.getType().equals(record.getApplyStatus())) {
                        ids.add(record.getId());
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(ids)) {
                List<Map> roleMap = sysRoleDataPermissionMapper.queryRoleByDataIds(ids);
                Map<Integer, List<Map>> groupMap = roleMap.stream()
                        .collect(Collectors.groupingBy(s -> Integer.parseInt(s.get("data_id").toString())));
                for (TableInfo record : page.getRecords()) {
                    List<Map> listMap = groupMap.get(record.getId());
                    if (listMap != null) {
                        List<String> names = listMap.stream().map(s -> s.get("role_name").toString())
                                .collect(Collectors.toList());
                        record.setApproveUser(String.join(",", names));
                    }
                }
            }
            List<TableInfo> list = baseMapper.selectLAllSizeList(tenantId);
            if (CollectionUtils.isNotEmpty(list)) {
                Double storeTotal = list.stream().filter(s -> s.getTotalSize() != null)
                        .mapToDouble(TableInfo::getTotalSize).sum();
                Double temp = storeTotal / (1024 * 1024 * 1024);
                if (temp < 1) {
                    temp = storeTotal / (1024 * 1024);
                    data.put("unit", "M");
                } else {
                    data.put("unit", "G");
                }
                data.put("storeTotal", temp.intValue());
            }
            Integer count = bdpRealTableInfoMapper.selectCount(new LambdaQueryWrapper<BdpRealTableInfo>()
                    .eq(BdpRealTableInfo::getDeleteFlag, 0)
                    .eq(BdpRealTableInfo::getTableType, 1)
                    .eq(BdpRealTableInfo::getTenantId, tenantId)
            );
            Long aLong = Long.valueOf(count);

            data.put("page", new PageUtils(page));
            data.put("tableTotal", page.getTotal() + aLong);
        }
        //实时
        if (params.get("flag") != null && params.get("flag").equals(2)) {
           /* if (!DataPermissionUtil.isAdmin(userId.intValue())) {
                params.put("userId", userId);
                List<Integer> resourceIds = DataPermissionUtil.getDataIds(PermissionTypeEnum.RESOURCE.getType(),
                        PermissionTypeEnum.QUERY.getType());
                if (CollectionUtils.isEmpty(resourceIds)) {
                    resourceIds.add(-1);//无权限
                }
                params.put("resourceIds", resourceIds);
                List<Integer> ids = DataPermissionUtil.getDataIds(PermissionTypeEnum.REAL_TABLE.getType(),
                        PermissionTypeEnum.QUERY.getType());
                if (CollectionUtils.isEmpty(ids)) {
                    ids.add(-1);//无权限
                }
                params.put("ids", ids);
            }*/
            //获取实时表申请列表
            getRealTableApplyList(params, data);
        }

        return data;
    }

    private void getRealTableApplyList(Map<String, Object> params, JSONObject data) {
        IPage<BdpRealTableInfo> page = bdpRealTableInfoMapper.selectRealApplyPage((Page<BdpRealTableInfo>) new Query<BdpRealTableInfo>().getPage(params), params);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            for (BdpRealTableInfo record : page.getRecords()) {
                if (StringUtils.isNotEmpty(record.getProjectName())) {
                    String s = record.getProjectName().replaceAll(",", "、");
                    record.setProjectName(s);
                }
                if (record.getApplyStatus() == null) {
                    record.setApplyStatus(ApplyStatusEnum.NONE.getType());
                }
            }
        }
        data.put("page", new PageUtils(page));
        data.put("tableTotal", page.getTotal());
    }

    @Override
    public PageUtils<TableInfo> selectListPage(Map<String, Object> params) {
        if (params.get("proc_id") == null) {//如果项目id 为空 需要数据权限过滤 不为空以项目权限为准
            DataPermissionUtil.putDataFilter(params,
                    PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        }
        IPage<TableInfo> page = this.baseMapper.selectListPage((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        return new PageUtils<>(page);
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }


    /**
     * 初始化表表权限  当前登录用户创建的表和字段 默认初始化当前登录用户的第一个角色的权限
     */
    @Override
    public void initDataPermission(Integer tableId, Long roleId, Integer userId) {
        List<TableInfoPermission> dataList = Lists.newArrayList();
        if (null == userId) {
            userId = ShiroUtils.getUserId().intValue();
        }
        List<Long> roleIds = new ArrayList<>();
        TableInfo table = getTableById(tableId);
        if (table == null) {
            return;
        }
        if (roleId != null) {
            roleIds.add(roleId);//给角色初始化权限
        }
        if (CollectionUtil.isNotEmpty(roleIds)) {
            TableInfoPermission tp = new TableInfoPermission();
            roleId = roleIds.get(0);
            if (userId.equals(table.getCreatePer())) {
                tp.yesValue(roleId, tableId, PermissionTypeEnum.STORE.getType());
            } else {
                SysRoleDataPermissionExample param = new SysRoleDataPermissionExample();
                param.createCriteria().andDataTypeEqualTo(PermissionTypeEnum.STORE.getType())
                        .andRoleIdEqualTo(roleId)
                        .andDataIdEqualTo(Long.valueOf(tableId))
                        .andQueryEqualTo(YesOrNoEnum.YES.getValue());
                List<SysRoleDataPermission> list = sysRoleDataPermissionMapper.selectByExample(param);
                if (CollectionUtils.isNotEmpty(list)) {
                    tp = new TableInfoPermission(table, list.get(0));
                } else {
                    tp.defaultValue(roleId, tableId, PermissionTypeEnum.STORE.getType());
                }
            }
            tp.setEnName(table.getTableName());
            tp.setChName(table.getTableAlias());
            SysRoleDataPermissionExample fieldParam = new SysRoleDataPermissionExample();
            fieldParam.createCriteria().andDataTypeEqualTo(PermissionTypeEnum.STORE_FILED.getType())
                    .andRoleIdEqualTo(roleId)
                    .andQueryEqualTo(YesOrNoEnum.YES.getValue());
            List<SysRoleDataPermission> authFields = sysRoleDataPermissionMapper.selectByExample(fieldParam);
            List<Integer> authFieldIds=new ArrayList<>();
            if(CollectionUtil.isNotEmpty(authFields)){
                authFieldIds=authFields.stream().map(i->i.getDataId()).collect(Collectors.toList());
            }
            if (CollectionUtils.isNotEmpty(table.getTableFieldInfos())) {
                //查询自己的创建的字段信息
                Integer finalUserId = userId;
                List<Integer> fieldIds=authFieldIds;
                List<TableFieldInfo> fieldList = table.getTableFieldInfos().stream()
                        .filter(s -> finalUserId.equals(s.getCreatePer())||fieldIds.contains(s.getId())).filter(distinctByKey(i->i.getId())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(fieldList)) {
                    ArrayList<TableFieldInfoPermission> fieldPerList = Lists.newArrayList();
                    for (TableFieldInfo tableFieldInfo : fieldList) {
                        TableFieldInfoPermission field = new TableFieldInfoPermission();
                        field.yesValue(roleId, tableFieldInfo.getId(), PermissionTypeEnum.STORE_FILED.getType());
                        field.setEnName(tableFieldInfo.getFieldName());
                        field.setChName(tableFieldInfo.getFieldName());
                        fieldPerList.add(field);
                    }
                    tp.setTableFieldInfos(fieldPerList);
                    dataList.add(tp);
                    savePermission(dataList);
                }
            }
        }

        //初始化表的创建者和当前租户的租户管理员的权限
        Set<String> intiCreateTable = Sets.newHashSet();
        intiCreateTable.add(table.getDbName() + "|" + table.getTableName()
                + "|" + table.getId() + "|" + table.getCreatePer());
        //当前租户的租户管理员
        Integer tenantId = ShiroUtils.getTenantId();
        List<SysTenantUser> users = sysTenantUserService.list(new LambdaQueryWrapper<SysTenantUser>().eq(SysTenantUser::getTenantId, tenantId).eq(SysTenantUser::getTenantType, 0));
        if (CollectionUtil.isNotEmpty(users)) {
            for (SysTenantUser user : users) {
                intiCreateTable.add(table.getDbName() + "|" + table.getTableName()
                        + "|" + table.getId() + "|" + user.getUserId());
            }
        }
        rangerDataService.intiCreateTable(intiCreateTable);
        List<DataApplication> applyList = dataApplicationMapper.selectListByTableId(tableId, 2);
        if (CollectionUtil.isNotEmpty(applyList)) {
            for (DataApplication dataApplication : applyList) {
                rangerDataService.saveOrUpdateRangerPolicy(dataApplication);
            }
        }
    }


    @Transactional
    @Override
    public void savePermission(List<TableInfoPermission> dataList) {
        if (CollectionUtils.isNotEmpty(dataList)) {
            List<SysRoleDataPermission> permissionList = new ArrayList<>(dataList.size());
            for (TableInfoPermission tableInfoPermission : dataList) {
                SysRoleDataPermission permission = tableInfoPermission.toPermission(tableInfoPermission);
                permissionList.add(permission);
                if (CollectionUtils.isNotEmpty(tableInfoPermission.getTableFieldInfos())) {
                    for (TableFieldInfoPermission tableFieldInfo : tableInfoPermission.getTableFieldInfos()) {
                        SysRoleDataPermission fieldPermission = tableFieldInfo.toPermission(tableFieldInfo);
                        permissionList.add(fieldPermission);
                    }
                }
            }
            dataPermissionService.savePermission(permissionList);
            rangerDataService.saveOrUpdateRangerPolicy(dataList);
        }
    }

    @Transactional
    @Override
    public void saveTenantPermission(List<TenantTableInfoPermission> dataList) {
        if (CollectionUtils.isNotEmpty(dataList)) {
            List<SysTenantDataPermission> permissionList = new ArrayList<>(dataList.size());

            for (TenantTableInfoPermission tableInfoPermission : dataList) {
                SysTenantDataPermission permission = tableInfoPermission.toPermission(tableInfoPermission);
                permissionList.add(permission);
                if (CollectionUtils.isNotEmpty(tableInfoPermission.getTableFieldInfos())) {
                    for (TenantTableFieldInfoPermission tableFieldInfo : tableInfoPermission.getTableFieldInfos()) {
                        SysTenantDataPermission fieldPermission = tableFieldInfo.toPermission(tableFieldInfo);
                        permissionList.add(fieldPermission);
                    }
                }
            }
            dataPermissionService.saveTenantPermission(permissionList);
            if (permissionList.size() == 1 && permissionList.get(0).getDataType() == 3) {//单独修改字段权限
                Integer tenantId = dataList.get(0).getTenantId();
                Integer fieldId = permissionList.get(0).getDataId();
                TableFieldInfo field = tableFieldInfoMapper.selectById(fieldId);
                List<TableInfo> records = tableInfoMapper.selectByIds(Collections.singleton(field.getTableId()));
                List<TenantTableInfoPermission> allDataList = getTenantDataList(tenantId, records);
                rangerDataService.saveOrUpdateRangerPolicyByTenant(allDataList);
            } else {
                rangerDataService.saveOrUpdateRangerPolicyByTenant(dataList);
            }
        }
    }

    /**
     * 根据角色id 和表数据 查询和生成权限信息
     *
     * @param roleId  角色id
     * @param records 表信息
     */
    private List<TableInfoPermission> getDataList(Long roleId, List<TableInfo> records) {
        // 查询数据权限信息
        Map<String, SysRoleDataPermission> permissionMap = Maps.newHashMap();
        if (roleId != null) {
            List<Integer> dataTypeList = new ArrayList<>(2);
            dataTypeList.add(PermissionTypeEnum.STORE.getType());
            dataTypeList.add(PermissionTypeEnum.STORE_FILED.getType());
            SysRoleDataPermissionExample param = new SysRoleDataPermissionExample();
            param.createCriteria().andDataTypeIn(dataTypeList)
                    .andRoleIdEqualTo(roleId);
            List<SysRoleDataPermission> list = sysRoleDataPermissionMapper.selectByExample(param);
            if (CollectionUtils.isNotEmpty(list)) {
                for (SysRoleDataPermission sysRoleDataPermission : list) {
                    permissionMap.put(sysRoleDataPermission.getDataId()
                                    + "-" + sysRoleDataPermission.getDataType(),
                            sysRoleDataPermission);
                }
            }
        }
        List<TableInfoPermission> dataList = new ArrayList<>();
        //查询字段信息
        Set<Integer> tableIds = Sets.newHashSet();
        for (TableInfo record : records) {
            tableIds.add(record.getId());
        }
        Integer userId = ShiroUtils.getUserId().intValue();
        List<TableFieldInfo> fieldList = tableFieldInfoMapper.selectListByTableIds(tableIds);
        //按照权限过滤字段
        List<Integer> ids = DataPermissionUtil.getDataIds(userId,
                PermissionTypeEnum.STORE_FILED.getType(), PermissionTypeEnum.ASSIGN.getType());
        fieldList = fieldList.stream().filter(
                s -> ids.contains(s.getId()) || userId.equals(s.getCreatePer())
        ).collect(Collectors.toList());
        Map<Integer, List<TableFieldInfo>> groupByMaps = fieldList.stream().collect(Collectors.groupingBy(TableFieldInfo::getTableId));
        for (TableInfo record : records) {
            List<TableFieldInfo> tableFieldList = groupByMaps.get(record.getId());
            List<TableFieldInfoPermission> tableFieldPerList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tableFieldList)) {
                for (TableFieldInfo tableFieldInfo : tableFieldList) {
                    SysRoleDataPermission permission = permissionMap.get(tableFieldInfo.getId() + "-"
                            + PermissionTypeEnum.STORE_FILED.getType());
                    if (permission != null) {
                        TableFieldInfoPermission tp = new TableFieldInfoPermission(tableFieldInfo, permission);
                        tableFieldPerList.add(tp);
                    } else {
                        TableFieldInfoPermission tp = new TableFieldInfoPermission(tableFieldInfo, null);
                        tp.defaultValue(roleId, tableFieldInfo.getId(), PermissionTypeEnum.STORE_FILED.getType());
                        tableFieldPerList.add(tp);
                    }
                }
            }
            SysRoleDataPermission permission = permissionMap.get(record.getId() + "-"
                    + PermissionTypeEnum.STORE.getType());
            if (permission != null) {
                TableInfoPermission table = new TableInfoPermission(record, permission);
                table.setTableFieldInfos(tableFieldPerList);
                dataList.add(table);
            } else {
                TableInfoPermission table = new TableInfoPermission(record, null);
                table.setTableFieldInfos(tableFieldPerList);
                table.defaultValue(roleId, record.getId(), PermissionTypeEnum.STORE.getType());
                dataList.add(table);
            }
        }
        return dataList;
    }

    /**
     * 根据租户id 和表数据 查询和生成权限信息
     *
     * @param tenantId 租户id
     * @param records  表信息
     */
    private List<TenantTableInfoPermission> getTenantDataList(Integer tenantId, List<TableInfo> records) {
        // 查询数据权限信息
        Map<String, SysTenantDataPermission> permissionMap = Maps.newHashMap();
        if (tenantId != null) {
            List<Integer> dataTypeList = new ArrayList<>(2);
            dataTypeList.add(PermissionTypeEnum.STORE.getType());
            dataTypeList.add(PermissionTypeEnum.STORE_FILED.getType());
            SysTenantDataPermissionExample param = new SysTenantDataPermissionExample();
            param.createCriteria().andDataTypeIn(dataTypeList)
                    .andTenantIdEqualTo(tenantId);
            List<SysTenantDataPermission> list = sysTenantDataPermissionMapper.selectByExample(param);
            if (CollectionUtils.isNotEmpty(list)) {
                for (SysTenantDataPermission sysTenantDataPermission : list) {
                    permissionMap.put(sysTenantDataPermission.getDataId()
                                    + "-" + sysTenantDataPermission.getDataType(),
                            sysTenantDataPermission);
                }
            }
        }
        List<TenantTableInfoPermission> dataList = new ArrayList<>();
        //查询字段信息
        Set<Integer> tableIds = Sets.newHashSet();
        for (TableInfo record : records) {
            tableIds.add(record.getId());
        }
        Integer userId = ShiroUtils.getUserId().intValue();
        List<TableFieldInfo> fieldList = tableFieldInfoMapper.selectListByTableIds(tableIds);
        if (!DataPermissionUtil.isAdmin(userId)) {
            //按照权限过滤字段
            List<Integer> ids = DataPermissionUtil.getDataIds(userId,
                    PermissionTypeEnum.STORE_FILED.getType(), PermissionTypeEnum.ASSIGN.getType());
            fieldList = fieldList.stream().filter(
                    s -> ids.contains(s.getId()) || userId.equals(s.getCreatePer())
            ).collect(Collectors.toList());
        }
        Map<Integer, List<TableFieldInfo>> groupByMaps = fieldList.stream().collect(Collectors.groupingBy(TableFieldInfo::getTableId));
        for (TableInfo record : records) {
            List<TableFieldInfo> tableFieldList = groupByMaps.get(record.getId());
            List<TenantTableFieldInfoPermission> tableFieldPerList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tableFieldList)) {
                for (TableFieldInfo tableFieldInfo : tableFieldList) {
                    SysTenantDataPermission permission = permissionMap.get(tableFieldInfo.getId() + "-"
                            + PermissionTypeEnum.STORE_FILED.getType());
                    if (permission != null) {
                        TenantTableFieldInfoPermission tp = new TenantTableFieldInfoPermission(tableFieldInfo, permission);
                        tableFieldPerList.add(tp);
                    } else {
                        TenantTableFieldInfoPermission tp = new TenantTableFieldInfoPermission(tableFieldInfo, null);
                        tp.defaultValue(tenantId, tableFieldInfo.getId(), PermissionTypeEnum.STORE_FILED.getType());
                        tableFieldPerList.add(tp);
                    }
                }
            }
            SysTenantDataPermission permission = permissionMap.get(record.getId() + "-"
                    + PermissionTypeEnum.STORE.getType());
            if (permission != null) {
                TenantTableInfoPermission table = new TenantTableInfoPermission(record, permission);
                table.setTableFieldInfos(tableFieldPerList);
                dataList.add(table);
            } else {
                TenantTableInfoPermission table = new TenantTableInfoPermission(record, null);
                table.setTableFieldInfos(tableFieldPerList);
                table.defaultValue(tenantId, record.getId(), PermissionTypeEnum.STORE.getType());
                dataList.add(table);
            }
        }
        return dataList;
    }

    @Override
    public PageUtils listPageByRole(Map<String, Object> params) {
        if (params.get("role_id") == null || StrUtil.isBlank(params.get("role_id").toString())|| Long.valueOf(params.get("role_id").toString()) < 1) {
            return new PageUtils<>(new ArrayList<>(),0,10,1);
        }
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.ASSIGN.getType());
        if (params.get("category_id") != null) {//添加类目查询条件
            Integer category_id = Integer.parseInt(params.get("category_id").toString());
            List<Integer> ids = baseMapper.selectTableIds(category_id);
            if (CollectionUtils.isEmpty(ids)) {//为空时
                ids = new ArrayList<>(1);
                ids.add(-1);
            }
            params.put("category_ids", ids);
        }
        if(params.get("cateGoryId")!=null){
            String cateGoryId = (String) params.get("cateGoryId");
            if(StrUtil.isNotBlank(cateGoryId)){
                List<Integer> list = this.baseMapper.selectTableInfoIds(Integer.valueOf(cateGoryId) );
                if(list.size()==0){
                    list.add(-1);
                }
                List<Integer> ids =(List<Integer>) params.get("ids");
                List<Integer> intersection = ids.stream().filter(item -> list.contains(item)).collect(Collectors.toList());
                if(intersection.size()==0){
                    intersection.add(-1);
                }
                params.put("ids", intersection);
            }
        }
        //查询表信息
//        setTenantParams(params,PermissionTypeEnum.STORE.getType(),PermissionTypeEnum.ASSIGN.getType(),ShiroUtils.getTenantId());
        IPage<TableInfo> page = this.baseMapper.listNoRelation((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        List<TableInfoPermission> dataList = new ArrayList<>();
        Long roleId = 0L;
        if (params.get("role_id") != null) {
            roleId = Long.parseLong(params.get("role_id").toString());
        }
        List<TableInfo> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            dataList = getDataList(roleId, records);
        }
        return new PageUtils(dataList, (int) page.getTotal(),
                (int) page.getSize(), (int) page.getCurrent());
    }

    public void setTenantParams(Map<String, Object> params, Integer datatype, Integer operation, Integer tenantId) {
        if (tenantId != null) {
            params.put("currentTenantId", tenantId);
            List<Integer> dataTypeList = new ArrayList<>(2);
            dataTypeList.add(datatype);
            SysTenantDataPermissionExample param = new SysTenantDataPermissionExample();
            param.createCriteria().andDataTypeIn(dataTypeList)
                    .andTenantIdEqualTo(ShiroUtils.getTenantId());
            if (operation == PermissionTypeEnum.ASSIGN.getType()) {
                param.getOredCriteria().get(0).andAssignEqualTo(1);
            }
            if (operation == PermissionTypeEnum.QUERY.getType()) {
                param.getOredCriteria().get(0).andQueryEqualTo(1);
            }
            List<SysTenantDataPermission> list = sysTenantDataPermissionMapper.selectByExample(param);
            if (CollectionUtils.isNotEmpty(list)) {
                List<Integer> dataIds = list.stream().map(i -> i.getDataId()).collect(Collectors.toList());
                params.put("dataIds", dataIds);
            }
        }
    }

    @Override
    public PageUtils listPageByTenant(Map<String, Object> params) {
        Long userId = ShiroUtils.getUserId();
        if (!DataPermissionUtil.isAdmin(userId.intValue())) {
            params.put("userId", userId);
        }
        if (params.get("category_id") != null
                && StringUtils.isNotEmpty(params.get("category_id").toString())) {//添加类目查询条件
            Integer category_id = Integer.parseInt(params.get("category_id").toString());
            List<Integer> ids = baseMapper.selectTableIds(category_id);
            if (CollectionUtils.isEmpty(ids)) {//为空时
                ids = new ArrayList<>(1);
                ids.add(-1);
            }
            params.put("tableIds", ids);
        }
        //查询表信息
        params.put("currentTenantId", ShiroUtils.getTenantId());
        IPage<TableInfo> page = this.baseMapper.listNoRelationByTenant((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        List<TenantTableInfoPermission> dataList = new ArrayList<>();
        Integer roleId = 0;
        if (params.get("tenant_id") != null) {
            roleId = Integer.parseInt(params.get("tenant_id").toString());
        }
        List<TableInfo> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            dataList = getTenantDataList(roleId, records);
        }
        return new PageUtils(dataList, (int) page.getTotal(),
                (int) page.getSize(), (int) page.getCurrent());
    }

    @Override
    public List<TableFieldInfo> selectListByDbNamesAndTableNames(Set<String> dbNames, Set<String> tableNames, Integer operation) {
        Map<String, Object> params = new HashMap<>();

        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), operation);
        if (CollectionUtils.isEmpty(dbNames) || CollectionUtils.isEmpty(tableNames)) {
            return new ArrayList<>();
        }
        params.put("dbNames", dbNames);
        params.put("tableNames", tableNames);
        List<TableInfo> list = tableInfoMapper.selectListByDbNamesAndTableNames(params);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        Set<Integer> tableIds = list.stream().map(TableInfo::getId).collect(Collectors.toSet());
        List<TableFieldInfo> fieldList = tableFieldInfoMapper.selectListByTableIds(tableIds);
        Long userId = ShiroUtils.getUserId();
        if (userId == null) {
            return fieldList;
        }
        if (!DataPermissionUtil.isAdmin(userId.intValue())) {//判断管理员
            //先判断申请权限
            List<Integer> applyDataIds = DataPermissionUtil.getApplyDataIds(userId, operation);//拥有申请权限
            //记录有权限的字段ids
            Set<Integer> ownIds = fieldList.stream().filter(s -> s.getCreatePer() != null)
                    .filter(s -> userId.equals(s.getCreatePer().longValue()))
                    .map(TableFieldInfo::getId).collect(Collectors.toSet());
            List<Integer> haveIds = new ArrayList<>(ownIds);
            //解析那些字段有权限
            Map<Integer, List<TableFieldInfo>> listMap = fieldList.stream().collect(Collectors.groupingBy(TableFieldInfo::getTableId));
            for (Integer tableId : listMap.keySet()) {
                if (!applyDataIds.contains(tableId)) {//没有申请表权限
                    //判断数据权限
                    List<Integer> ids = DataPermissionUtil.getDataIds(userId.intValue(),
                            PermissionTypeEnum.STORE_FILED.getType(), operation);
                    //查询是否 提交字段查新建字段的权限
                    ids.addAll(DataPermissionUtil.getCreateFiledIds(tableId, userId, StoreTableType.OFFLINE.getCode()));
                    if (CollectionUtils.isNotEmpty(ids)) {
                        haveIds.addAll(ids);
                    }
                } else {//有申请表的权限 此表全部字段都有权限
                    List<Integer> ids = listMap.get(tableId).stream().map(TableFieldInfo::getId).collect(Collectors.toList());
                    haveIds.addAll(ids);
                }
            }
            if (CollectionUtils.isNotEmpty(haveIds)) {
                fieldList = fieldList.stream().filter(s -> (haveIds.contains(s.getId()))
                ).collect(Collectors.toList());
            }
        }

        return fieldList;
    }


    @Override
    public List<TableInfo> selectList(Map<String, Object> params) {
        if (params.get("proc_id") == null) {//如果项目id 为空 需要数据权限过滤 不为空以项目权限为准
            DataPermissionUtil.putDataFilter(params,
                    PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        }
        return this.baseMapper.selectListPage(params);
    }

    @Override
    public List<TableInfo> listTableByStorageEngine(Map<String, Object> params) {
        if (params.get("proc_id") == null) {//如果项目id 为空 需要数据权限过滤 不为空以项目权限为准
            DataPermissionUtil.putDataFilter(params,
                    PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        }
        return this.baseMapper.listTableByStorageEngine(params);
    }

    @Override
    public PageUtils<TableInfo> listNoRelation(Map<String, Object> params) {
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
//        tableInfoService.setTenantParams(params,PermissionTypeEnum.STORE.getType(),PermissionTypeEnum.QUERY.getType(),ShiroUtils.getTenantId());
        IPage<TableInfo> page = this.baseMapper.listNoRelation((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        return new PageUtils<>(page);
    }

    @Override
    public void checkedAnalysisInfo(Integer id, int type) {
        List<AnalysisInfo> analysisInfos = analysisInfoMapper.selectList(new QueryWrapper<AnalysisInfo>().eq("table_info_id", id));
        if (type == 1) {
            if (null != analysisInfos && analysisInfos.size() > 0) {
                throw new RRException(DataDevelopmentBizExceptionEnum.LINK_ANALYSIS_JOBINFO_EXIST.getMessage());
            }
        }
        if (type == 2) {
            if (null != analysisInfos && analysisInfos.size() > 0) {
                long count = analysisInfos.stream().filter(analysisInfo -> Objects.equals(true, analysisInfo.getEnable())).count();
                if (count > 0) {
                    throw new RRException(DataDevelopmentBizExceptionEnum.LINK_ANALYSIS_JOBINFO_RUNNING.getMessage());
                }
            }
        }
    }

    @Override
    public List<Integer> getIdsByNames(List<String> tableNames, String dbName) {
        return tableInfoMapper.getTableIdsByNames(tableNames, dbName);
    }

    @Override
    public List<TableAndConlumDto> getAllCoulonmsByUserId(Long userId, List<IdAndNameDto> tableIds) throws Exception {
        if (CollectionUtil.isEmpty(tableIds)) {
            return null;
        }
        //1.这个表是否这个用户是否可以看到：是否是自己的表，是否是加入了这个项目的人的表
        cheackIsMyTableOrAddProTable(userId, tableIds);

        //2.验证这些表对应的库是否正确
        SysUserDto sysUserDto = sysUserRoleService.queryUserInfoByUserId(userId, ShiroUtils.getTenantId());
        if (null == sysUserDto) {
            throw new RRException(DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getMessage(), DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getCode());
        }
        if (CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.ROLE_NOTFOUND.getMessage(), DataDevelopmentBizExceptionEnum.ROLE_NOTFOUND.getCode());
        }
        Set<Integer> allDbIds = new HashSet<>();
        Set<Integer> alltableIds = new HashSet<>();
        Set<Integer> allConlumIds = new HashSet<>();
        initDbAndTableAndConlumIds(allDbIds, alltableIds, allConlumIds, null, sysUserDto);

        //验证角色对应的库权限
        cheackAllRoleDb(allDbIds, tableIds);

        //验证角色对应的表权限
        cheackAllRoleTable(alltableIds, tableIds);

        //3.判断对应的这些表可以看到的字段，并且直接返回
        List<TableAndConlumDto> restList = pickAllRoleConlum(allConlumIds, tableIds);

        if (CollectionUtil.isEmpty(restList)) {
            throw new RRException(String.format(DataDevelopmentBizExceptionEnum.TABLE_NO_COLUMN_NO_PREMISSION.getMessage(), tableIds.get(0).getName()));
        }

        return restList;
    }

    @Override
    public void initDbAndTableAndConlumIds(Set<Integer> allDbIds, Set<Integer> alltableIds, Set<Integer> allConlumIds, Set<RoleRowDataPermissionDto> roleRowDataPermissionDtos, SysUserDto sysUserDto) {
        if (CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
            return;
        }
        sysUserDto.getRoleIdList().forEach(roleId -> {
            RoleDataPermissionDto roleDataPermissionDto = iRoleDataPermissionConfigService.queryRoleDataPermissionDataByRoleId(roleId);
            if (null != roleDataPermissionDto) {
                Set<Integer> rolePreminssionSet1 = changeData(roleDataPermissionDto.getDbIds());
                if (CollectionUtil.isNotEmpty(rolePreminssionSet1)) {
                    allDbIds.addAll(rolePreminssionSet1);
                }
                Set<Integer> rolePreminssionSet2 = changeData(roleDataPermissionDto.getTableLevelIds());
                if (CollectionUtil.isNotEmpty(rolePreminssionSet2)) {
                    alltableIds.addAll(rolePreminssionSet2);
                }
                Set<Integer> rolePreminssionSet3 = changeData(roleDataPermissionDto.getColoumLevelIds());
                if (CollectionUtil.isNotEmpty(rolePreminssionSet3)) {
                    allConlumIds.addAll(rolePreminssionSet3);
                }

                if (null != roleRowDataPermissionDtos) {
                    if (CollectionUtil.isNotEmpty(roleDataPermissionDto.getAllRowDataPermission())) {
                        roleRowDataPermissionDtos.addAll(roleDataPermissionDto.getAllRowDataPermission());
                        //查询到对应的数据，并且封装成数据集合
                        List<RoleRowDataPermissionDto> allRowDataPermissionList = roleDataPermissionDto.getAllRowDataPermission();
                        for (RoleRowDataPermissionDto roleRowDataPermissionDto : allRowDataPermissionList) {
                            RoleRowDataPermissionDto roleRowDataPermissionDto1 = tableFieldRulesMapper.selectRoleRowDataPermissionDtoById(roleRowDataPermissionDto.getTableFieldRulesId());
                            if (null != roleRowDataPermissionDto1) {
                                roleRowDataPermissionDto1.setRoleDataPermissionId(roleRowDataPermissionDto.getRoleDataPermissionId());
                                roleRowDataPermissionDtos.add(roleRowDataPermissionDto1);
                            }
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initDbAndTableAndColumIds(Set<Integer> allDbIds, Set<Integer> alltableIds, Set<Integer> allConlumIds, Set<RoleRowDataPermissionDto> roleRowDataPermissionDtos, Long roleId) {
        List<Long> roleIdList = Arrays.asList(roleId);
        SysUserDto sysUserDto = new SysUserDto();
        sysUserDto.setRoleIdList(roleIdList);
        initDbAndTableAndConlumIds(allDbIds, alltableIds, allConlumIds, roleRowDataPermissionDtos, sysUserDto);
    }

    @Override
    public void initDbAndTable(Set<Integer> allDbIds, Set<Integer> allTableIds, Long roleId) {
        RoleDataPermissionDto roleDataPermissionDto = iRoleDataPermissionConfigService.queryRoleDataPermissionDataByRoleId(roleId);
        if (null != roleDataPermissionDto) {
            Set<Integer> rolePreminssionSet1 = changeData(roleDataPermissionDto.getDbIds());
            if (CollectionUtil.isNotEmpty(rolePreminssionSet1)) {
                allDbIds.addAll(rolePreminssionSet1);
            }
            Set<Integer> rolePreminssionSet2 = changeData(roleDataPermissionDto.getTableLevelIds());
            if (CollectionUtil.isNotEmpty(rolePreminssionSet2)) {
                allTableIds.addAll(rolePreminssionSet2);
            }
        }
    }

    @Override
    public void initDbAndTableAndColum(Set<Integer> allDbIds, Set<Integer> allTableIds, Set<Integer> allConlumIds, Long roleId) {
        RoleDataPermissionDto roleDataPermissionDto = iRoleDataPermissionConfigService.queryRoleDataPermissionDataByRoleId(roleId);
        if (null != roleDataPermissionDto) {
            Set<Integer> rolePreminssionSet1 = changeData(roleDataPermissionDto.getDbIds());
            if (CollectionUtil.isNotEmpty(rolePreminssionSet1)) {
                allDbIds.addAll(rolePreminssionSet1);
            }
            Set<Integer> rolePreminssionSet2 = changeData(roleDataPermissionDto.getTableLevelIds());
            if (CollectionUtil.isNotEmpty(rolePreminssionSet2)) {
                allTableIds.addAll(rolePreminssionSet2);
            }
            Set<Integer> rolePreminssionSet3 = changeData(roleDataPermissionDto.getColoumLevelIds());
            if (CollectionUtil.isNotEmpty(rolePreminssionSet3)) {
                allConlumIds.addAll(rolePreminssionSet3);
            }
        }
    }


    @Override
    public List<UserAndTableDto> packUserAndTableData(List<Long> userIds) {
        if (CollectionUtil.isEmpty(userIds)) {
            return null;
        }
        List<UserAndTableDto> resultList = new ArrayList<>();
        for (Long userId : userIds) {
            UserAndTableDto userAndTableDto = new UserAndTableDto();
            userAndTableDto.setUserId(userId);
            userAndTableDto.setAllTableAndFieldDtoList(haveTableAndTableFieldList(userId));
            resultList.add(userAndTableDto);
        }
        return resultList;
    }

    @Override
    public List<UserAndTableDto> packUserAndTableData(Integer tableId) {

        //查询到所有的这个用户对应的所有的表
        Set<Long> allUserIds = new HashSet<>();
        //先查询自己的表是否有不存在的。
        TableInfo tableInfo1 = tableInfoMapper.selectById(tableId);
        if (null == tableInfo1) {
            return null;
        }
        allUserIds.add(Long.valueOf(tableInfo1.getCreatePer()));
        //再查询加入的项目是否，关联度表是否存在
        List<Long> userIds = procUserMapper.selectByTableId(tableId);
        if (CollectionUtil.isNotEmpty(userIds)) {
            allUserIds.addAll(userIds);
        }
        if (CollectionUtil.isEmpty(allUserIds)) {
            return null;
        }

        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        TableAndFieldDto tableAndFieldDto = tableInfoMapper.getTableByIdToTable(tableId);
        if (null != tableAndFieldDto) {
            //表等级
            if (null == tableAndFieldDto.getTableLevelId()) {
                if (null != allPermissionConfig) {
                    tableAndFieldDto.setTableLevelId(allPermissionConfig.getDataPermissionId());
                }
            }
            //表的字段
            List<TableFieldDto> restTableFieldList = tableFieldInfoMapper.selectListByTableId(tableId);
            if (CollectionUtil.isNotEmpty(restTableFieldList)) {
                for (TableFieldDto tableFieldDto : restTableFieldList) {
                    if (null == tableFieldDto.getColcumLevelId()) {
                        if (null != allPermissionConfigCulonm) {
                            tableFieldDto.setColcumLevelId(allPermissionConfigCulonm.getDataPermissionId());
                        }
                    }
                }
                tableAndFieldDto.setAllFieldList(restTableFieldList);
            }
        }

        List<UserAndTableDto> resultList = new ArrayList<>();
        for (Long userId : allUserIds) {
            UserAndTableDto userAndTableDto = new UserAndTableDto();
            userAndTableDto.setUserId(userId);
            userAndTableDto.setAllTableAndFieldDtoList(Arrays.asList(tableAndFieldDto));
            resultList.add(userAndTableDto);
        }
        return resultList;
    }

    @Override
    public List<Long> packUserIdByTableId(Integer tableId) {

        //查询到所有的这个用户对应的所有的表
        Set<Long> allUserIds = new HashSet<>();
        //先查询自己的表是否有不存在的。
        TableInfo tableInfo1 = tableInfoMapper.selectById(tableId);
        if (null == tableInfo1) {
            return null;
        }
        allUserIds.add(Long.valueOf(tableInfo1.getCreatePer()));
        //再查询加入的项目是否，关联度表是否存在
        List<Long> userIds = procUserMapper.selectByTableId(tableId);
        if (CollectionUtil.isNotEmpty(userIds)) {
            allUserIds.addAll(userIds);
        }
        if (CollectionUtil.isEmpty(allUserIds)) {
            return null;
        }
        return new ArrayList<>(allUserIds);
    }

    @Override
    public Boolean cheackDataPressionMenu(Integer id, Long userId) {
        //这个表和当前用户是否可以看到这个数据安全的菜单权限
        TableInfo tableInfo = tableInfoMapper.selectOne(new QueryWrapper<TableInfo>()
                .eq("id", id)
                .eq("create_per", userId));
        if (null != tableInfo) {
            return true;
        }
        //项目创建者
        List<ProcTable> procTables = procTableMapper.selectList(new QueryWrapper<ProcTable>().eq("table_id", id));
        if (CollectionUtil.isNotEmpty(procTables)) {
            List<Integer> allProcList = new ArrayList<>();
            for (ProcTable procTable : procTables) {
                allProcList.add(procTable.getProcId());
            }
            //根据工程ids查询到所有的工程信息
            List<ProcInfo> allProcInfoList = procInfoMapper.selectListByIds(allProcList);
            if (CollectionUtil.isNotEmpty(allProcInfoList)) {
                Set<Long> allUserIds = new HashSet<>();
                for (ProcInfo procInfo : allProcInfoList) {
                    allUserIds.add(Long.valueOf(procInfo.getCreatePer()));
                }
                if (allUserIds.contains(userId)) {
                    return true;
                }
            }
        }
        return false;

    }

    @Override
    public void selectByUserId(Long userId) {
        List<TableInfo> tableInfos = tableInfoMapper.selectList(new QueryWrapper<TableInfo>().eq("create_per", userId));
        if (CollectionUtil.isNotEmpty(tableInfos)) {
            throw new RRException("用户" + userId + "存在仓库表，则不能删除!");
        }
    }

    @Override
    public List<TableInfo> selectListByDbNameAndTableName(String olderDbName, Set<String> tableNameSet) {
        return tableInfoMapper.selectListByDbNameAndTableName(olderDbName, tableNameSet);
    }

    private List<TableAndFieldDto> haveTableAndTableFieldList(Long userId) {
        SysUserDto sysUserDto = sysUserRoleService.queryUserInfoByUserId(userId, ShiroUtils.getTenantId());
        if (null == sysUserDto || CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
            return null;
        }
        //查询到所有的这个用户对应的所有的表
        Set<Integer> allTableIds = new HashSet<>();
        //先查询自己的表是否有不存在的。
        List<TableInfo> myTableList = tableInfoMapper.selectList(new QueryWrapper<TableInfo>().eq("create_per", userId));
        if (CollectionUtil.isNotEmpty(myTableList)) {
            myTableList.forEach(tableInfo -> {
                allTableIds.add(tableInfo.getId());
            });
        }
        //再查询加入的项目是否，关联度表是否存在
        List<Integer> proAllTableIds = procUserMapper.selectByUserId(userId);
        if (CollectionUtil.isNotEmpty(proAllTableIds)) {
            allTableIds.addAll(proAllTableIds);
        }
        if (CollectionUtil.isEmpty(allTableIds)) {
            return null;
        }

        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        //拼装对应的表和表的字段信息
        List<TableAndFieldDto> restList = new ArrayList<>();
        for (Integer tableId : allTableIds) {
            TableAndFieldDto tableAndFieldDto = tableInfoMapper.getTableByIdToTable(tableId);
            if (null != tableAndFieldDto) {
                //表等级
                if (null == tableAndFieldDto.getTableLevelId()) {
                    if (null != allPermissionConfig) {
                        tableAndFieldDto.setTableLevelId(allPermissionConfig.getDataPermissionId());
                    }
                }
                //表的字段
                List<TableFieldDto> restTableFieldList = tableFieldInfoMapper.selectListByTableId(tableId);
                if (CollectionUtil.isNotEmpty(restTableFieldList)) {
                    for (TableFieldDto tableFieldDto : restTableFieldList) {
                        if (null == tableFieldDto.getColcumLevelId()) {
                            if (null != allPermissionConfigCulonm) {
                                tableFieldDto.setColcumLevelId(allPermissionConfigCulonm.getDataPermissionId());
                            }
                        }
                    }
                    tableAndFieldDto.setAllFieldList(restTableFieldList);
                }
                restList.add(tableAndFieldDto);
            }
        }
        return restList;
    }

    @Override
    public List<Map<String, Object>> searchProjectList(Long userId) {
        Map<String, Object> params = Maps.newHashMap();
        List<Integer> ids = DataPermissionUtil.getDataIds(userId.intValue(), PermissionTypeEnum.STORE.getType(),
                PermissionTypeEnum.QUERY.getType());
        if (CollectionUtils.isNotEmpty(ids)) {
            params.put("ids", ids);
        } else {
            ids.add(-1);//无权限查询-1的数据
            params.put("ids", ids);
        }
        return this.baseMapper.searchProjectList(params);
    }

    @Override
    public JSONArray roleAndPermission(Map<String, Object> params, Long userId) {
        JSONArray array = new JSONArray();
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        params.put("page", "1");
        params.put("limit", Integer.MAX_VALUE + "");
        long start = System.currentTimeMillis();

        //查询表信息
//        tableInfoService.setTenantParams(params,PermissionTypeEnum.STORE.getType(),PermissionTypeEnum.QUERY.getType(),ShiroUtils.getTenantId());
        IPage<TableInfo> page = this.baseMapper.listNoRelation((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        long tableTime = System.currentTimeMillis();
        log.info("table cost  time {}", tableTime - start);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<TableInfo> myTableList = page.getRecords();
            Set<Integer> allTableIds = myTableList.stream().map(TableInfo::getId).collect(Collectors.toSet());
            //先判断是否申请表权限
            List<TableFieldInfo> fieldList = new ArrayList<>();
            if (DataPermissionUtil.isAdmin(userId.intValue())) {//超管不需要过滤权限
                fieldList = tableFieldInfoMapper.selectListByTableIds(allTableIds);
            } else {
                List<Integer> applyDataIds = DataPermissionUtil.getApplyDataIds(userId, PermissionTypeEnum.QUERY.getType());
                if (CollectionUtil.isNotEmpty(applyDataIds)) {
                    Set<Integer> tableHaveIds = allTableIds.stream().filter(applyDataIds::contains).collect(Collectors.toSet());
                    if (CollectionUtil.isNotEmpty(tableHaveIds)) {
                        fieldList = tableFieldInfoMapper.selectListByTableIds(tableHaveIds);
                    }
                }
                //判断数据权限
                List<Integer> ids = DataPermissionUtil.getDataIds(
                        userId.intValue(), PermissionTypeEnum.STORE_FILED.getType(), PermissionTypeEnum.QUERY.getType());
                Set<Integer> tableNoHaveIds = allTableIds.stream().filter(s -> !applyDataIds.contains(s)).collect(Collectors.toSet());
                if (CollectionUtil.isNotEmpty(tableNoHaveIds)) {
                    //判断申请的字段权限
                    for (Integer id : tableNoHaveIds) {
                        //申请的新增字段权限id
                        ids.addAll(DataPermissionUtil.getCreateFiledIds(id, userId, StoreTableType.OFFLINE.getCode()));
                    }
                    List<TableFieldInfo> fieldNoList = tableFieldInfoMapper.selectListByTableIds(tableNoHaveIds);
                    if (CollectionUtil.isNotEmpty(ids)) {
                        List<TableFieldInfo> fieldChildList = fieldNoList.stream().filter(
                                s -> ids.contains(s.getId()) || Objects.equals(userId.intValue(), s.getCreatePer())
                        ).collect(Collectors.toList());
                        fieldList.addAll(fieldChildList);
                    }
                }
            }
            long fieldTime = System.currentTimeMillis();
            log.info("field cost  time {}", fieldTime - tableTime);
            Map<String, List<TableInfo>> dbMap = myTableList.stream().collect(Collectors.groupingBy(TableInfo::getDbName));
            Map<Integer, List<TableFieldInfo>> fieldMap = fieldList.stream().collect(Collectors.groupingBy(TableFieldInfo::getTableId));
            long group = System.currentTimeMillis();
            log.info("table group cost  time {}", group - fieldTime);
            List<String> dbNames = new ArrayList<>(dbMap.keySet());
            dbNames.sort(String::compareTo);
            for (String dbName : dbNames) {
                JSONObject dbJson = new JSONObject();
                dbJson.put("label", dbName);
                dbJson.put("id", dbName);
                JSONArray tableArray = new JSONArray();
                if (CollectionUtils.isNotEmpty(dbMap.get(dbName))) {
                    List<TableInfo> childTableList = dbMap.get(dbName);
                    for (TableInfo tableInfo : childTableList) {
                        JSONObject tableObj = new JSONObject();
                        JSONArray fieldArray = new JSONArray();
                        tableObj.put("dbName", tableInfo.getDbName());
                        tableObj.put("tableAlias", tableInfo.getTableAlias());
                        tableObj.put("label", tableInfo.getTableName());
                        tableObj.put("format", tableInfo.getFormat());
                        tableObj.put("desc", tableInfo.getDesc());
                        tableObj.put("id", tableInfo.getId());
                        //看是否，不需要字段
                        if (!Objects.equals(1, params.get("noNeedTableField"))) {
                            if (CollectionUtils.isNotEmpty(fieldMap.get(tableInfo.getId()))) {
                                for (TableFieldInfo tableFieldInfo : fieldMap.get(tableInfo.getId())) {
                                    JSONObject fieldObj = new JSONObject();
                                    fieldObj.put("label", tableFieldInfo.getFieldName());
                                    fieldObj.put("fieldAlias", tableFieldInfo.getFieldAlias());
                                    fieldObj.put("fieldType", tableFieldInfo.getFieldType());
                                    fieldObj.put("isPartition", tableFieldInfo.getIsPartition());
                                    fieldArray.add(fieldObj);
                                }
                            }
                            tableObj.put("children", fieldArray);
                        }
                        tableArray.add(tableObj);
                    }
                }
                dbJson.put("children", tableArray);
                array.add(dbJson);
            }
            long wrapData = System.currentTimeMillis();
            log.info("wrapData cost  time {}", wrapData - group);
        }
        return array;
    }


    @Override
    public JSONArray listTreeNotField(Map<String, Object> params, Long userId) {
        JSONArray array = new JSONArray();
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        params.put("page", "1");
        params.put("limit", Integer.MAX_VALUE + "");
        long start = System.currentTimeMillis();

        //查询表信息
        IPage<TableInfo> page = this.baseMapper.listNoRelation((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        long tableTime = System.currentTimeMillis();
        log.info("table cost  time {}", tableTime - start);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<TableInfo> myTableList = page.getRecords();
            long fieldTime = System.currentTimeMillis();
            log.info("field cost  time {}", fieldTime - tableTime);
            Map<String, List<TableInfo>> dbMap = myTableList.stream().collect(Collectors.groupingBy(TableInfo::getDbName));
            long group = System.currentTimeMillis();
            log.info("table group cost  time {}", group - fieldTime);
            List<String> dbNames = new ArrayList<>(dbMap.keySet());
            dbNames.sort(String::compareTo);
            for (String dbName : dbNames) {
                JSONObject dbJson = new JSONObject();
                dbJson.put("label", dbName);
                dbJson.put("id", dbName);
                JSONArray tableArray = new JSONArray();
                if (CollectionUtils.isNotEmpty(dbMap.get(dbName))) {
                    List<TableInfo> childTableList = dbMap.get(dbName);
                    for (TableInfo tableInfo : childTableList) {
                        JSONObject tableObj = new JSONObject();
                        tableObj.put("dbName", tableInfo.getDbName());
                        tableObj.put("tableAlias", tableInfo.getTableAlias());
                        tableObj.put("label", tableInfo.getTableName());
                        tableObj.put("format", tableInfo.getFormat());
                        tableObj.put("desc", tableInfo.getDesc());
                        tableObj.put("id", tableInfo.getId());
                        tableArray.add(tableObj);
                    }
                }
                dbJson.put("children", tableArray);
                array.add(dbJson);
            }
            long wrapData = System.currentTimeMillis();
            log.info("wrapData cost  time {}", wrapData - group);
        }
        return array;
    }

    @Override
    public JSONArray listTreeField(Integer tableId, Long userId) {
        Set<Integer> allTableIds = new HashSet<>();
        allTableIds.add(tableId);
        //先判断是否申请表权限
        List<TableFieldInfo> fieldList = new ArrayList<>();
        if (DataPermissionUtil.isAdmin(userId.intValue())) {//超管不需要过滤权限
            fieldList = tableFieldInfoMapper.selectListByTableIds(allTableIds);
        } else {
            List<Integer> applyDataIds = DataPermissionUtil.getApplyDataIds(userId, PermissionTypeEnum.QUERY.getType());
            if (CollectionUtil.isNotEmpty(applyDataIds)) {
                Set<Integer> tableHaveIds = allTableIds.stream().filter(applyDataIds::contains).collect(Collectors.toSet());
                if (CollectionUtil.isNotEmpty(tableHaveIds)) {
                    fieldList = tableFieldInfoMapper.selectListByTableIds(tableHaveIds);
                }
            }
            //判断数据权限
            List<Integer> ids = DataPermissionUtil.getDataIds(
                    userId.intValue(), PermissionTypeEnum.STORE_FILED.getType(), PermissionTypeEnum.QUERY.getType());
            Set<Integer> tableNoHaveIds = allTableIds.stream().filter(s -> !applyDataIds.contains(s)).collect(Collectors.toSet());
            if (CollectionUtil.isNotEmpty(tableNoHaveIds)) {
                //判断申请的字段权限
                for (Integer id : tableNoHaveIds) {
                    //申请的新增字段权限id
                    ids.addAll(DataPermissionUtil.getCreateFiledIds(id, userId, StoreTableType.OFFLINE.getCode()));
                }
                List<TableFieldInfo> fieldNoList = tableFieldInfoMapper.selectListByTableIds(tableNoHaveIds);
                if (CollectionUtil.isNotEmpty(ids)) {
                    List<TableFieldInfo> fieldChildList = fieldNoList.stream().filter(
                            s -> ids.contains(s.getId()) || Objects.equals(userId.intValue(), s.getCreatePer())
                    ).collect(Collectors.toList());
                    fieldList.addAll(fieldChildList);
                }
            }
        }
        Map<Integer, List<TableFieldInfo>> fieldMap = fieldList.stream().collect(Collectors.groupingBy(TableFieldInfo::getTableId));
        JSONArray fieldArray = new JSONArray();
        if (CollectionUtils.isNotEmpty(fieldMap.get(tableId))) {
            for (TableFieldInfo tableFieldInfo : fieldMap.get(tableId)) {
                JSONObject fieldObj = new JSONObject();
                fieldObj.put("label", tableFieldInfo.getFieldName());
                fieldObj.put("fieldAlias", tableFieldInfo.getFieldAlias());
                fieldObj.put("fieldType", tableFieldInfo.getFieldType());
                fieldObj.put("isPartition", tableFieldInfo.getIsPartition());
                fieldArray.add(fieldObj);
            }
        }
        return fieldArray;
    }

    @Override
    public Integer setCollectionTableId(Integer tableId, Integer collectionTableId) {
        return this.baseMapper.setCollectionTableId(tableId,collectionTableId);
    }

    @Override
    public Integer deleteCategory(Integer tableId) {
        return this.baseMapper.deleteCategory(tableId);
    }

    @Override
    public Integer insertRelation(Map<String, Object> map) {
        return this.baseMapper.insertRelation(map);
    }

    @Override
    public List<String> selectCategoryIdsByTableId(Integer tableId) {
        return this.baseMapper.selectCategoryIdsByTableId(tableId);
    }


    @Deprecated
    public JSONArray roleAndPermission2(Map<String, Object> params, Long userId) {
        SysUserDto sysUserDto = sysUserRoleService.queryUserInfoByUserId(userId, ShiroUtils.getTenantId());
        if (null == sysUserDto) {
            throw new RRException(DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getMessage(), DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getCode());
        }
        if (CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
            throw new RRException(DataDevelopmentBizExceptionEnum.ROLE_NOTFOUND.getMessage(), DataDevelopmentBizExceptionEnum.ROLE_NOTFOUND.getCode());
        }
        //查询到所有的这个用户对应的所有的表
        Set<Integer> allTableIds = new HashSet<>();
        //先查询自己的表是否有不存在的。
        List<TableInfo> myTableList = this.baseMapper.selectList(new QueryWrapper<TableInfo>().eq("create_per", userId));
        if (CollectionUtil.isNotEmpty(myTableList)) {
            myTableList.forEach(tableInfo -> {
                allTableIds.add(tableInfo.getId());
            });
        }
        //再查询加入的项目是否，关联度表是否存在
        List<Integer> proAllTableIds = procUserMapper.selectByUserId(userId);
        if (CollectionUtil.isNotEmpty(proAllTableIds)) {
            allTableIds.addAll(proAllTableIds);
        }
        JSONArray array = new JSONArray();
        //查询到所有的表信息
        if (CollectionUtil.isEmpty(allTableIds)) {
            return array;
        }
        List<TableInfo> tableInfos = tableInfoMapper.selectByIds(allTableIds);
        if (CollectionUtil.isEmpty(tableInfos)) {
            return array;
        }
        //如果有选择对应的项目，则需要只查询对应的项目中的表信息
        List<Integer> tableIds = new ArrayList<>();
        if (null != params.get("proc_id")) {
            //查询到对应的工程的所有的表信息
            List<ProcTable> procTables = procTableMapper.selectList(new QueryWrapper<ProcTable>().eq("proc_id", params.get("proc_id")));
            if (CollectionUtil.isEmpty(procTables)) {
                return array;
            }
            procTables.forEach(procTable -> {
                tableIds.add(procTable.getTableId());
            });
            if (CollectionUtil.isEmpty(tableIds)) {
                return array;
            }
        }
        List<TableInfo> newTableInfo = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(tableIds)) {
            newTableInfo = tableInfos.stream().filter(tableInfo -> tableIds.contains(tableInfo.getId())).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(newTableInfo)) {
                return array;
            }
        }
        if (CollectionUtil.isNotEmpty(newTableInfo)) {
            tableInfos.clear();
            tableInfos.addAll(newTableInfo);
        }

        //先获取到所有的库名称可以看到的库名称
        Set<Integer> allDbIds = new HashSet<>();
        Set<Integer> alltableIds = new HashSet<>();
        Set<Integer> allConlumIds = new HashSet<>();
        initDbAndTableAndConlumIds(allDbIds, alltableIds, allConlumIds, null, sysUserDto);
        if (CollectionUtil.isEmpty(allDbIds)) {
            return array;
        }
        //得到对应的可以看到的数据库
        List<DataPermissionParam> allDataPermissionParamList = dataPermissionParamMapper.selectListByTypeAndIds(allDbIds, PressionParamType.DB.getId());
        if (CollectionUtil.isEmpty(allDataPermissionParamList)) {
            return array;
        }
        Set<String> allRoleDb = new HashSet<>();
        for (DataPermissionParam dataPermissionParam : allDataPermissionParamList) {
            allRoleDb.add(dataPermissionParam.getPermissName().toLowerCase());
        }
        Set<String> seeDbs = new HashSet<>();
        tableInfos.forEach(tableInfo -> {
            if (allRoleDb.contains(tableInfo.getDbName())) {
                seeDbs.add(tableInfo.getDbName());
            }
        });
        if (CollectionUtil.isEmpty(seeDbs)) {
            return array;
        }
        //这些数据库
        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        for (String seeDb : seeDbs) {
            JSONObject dbJson = new JSONObject();
            dbJson.put("label", seeDb);
            JSONArray tableArray = new JSONArray();
            dbJson.put("children", tableArray);
            array.add(dbJson);
            //查询children下的表名称
            for (TableInfo tableInfo : tableInfos) {
                if (!seeDb.equals(tableInfo.getDbName())) {
                    continue;
                }
                JSONArray fieldArray = new JSONArray();
                int flage = 0;
                if (null == tableInfo.getTableLevelId()) {
                    if (null != allPermissionConfig) {
                        if (alltableIds.contains(allPermissionConfig.getDataPermissionId())) {
                            flage = 1;
                            pingChildrenData(tableInfo, tableArray, fieldArray);
                        }
                    }
                } else {
                    if (alltableIds.contains(tableInfo.getTableLevelId())) {
                        flage = 1;
                        pingChildrenData(tableInfo, tableArray, fieldArray);
                    }
                }
                //看是否，不需要字段
                if (!Objects.equals(1, params.get("noNeedTableField"))) {
                    if (1 == flage) {
                        //拼装其表的字段是否可以查看
                        List<TableInfo> idAndNameDtoList = tableInfos.stream().filter(tableInfo1 -> Objects.equals(tableInfo.getId(), tableInfo1.getId())).collect(Collectors.toList());
                        if (CollectionUtil.isNotEmpty(idAndNameDtoList)) {
                            TableInfo tableInfo1 = idAndNameDtoList.get(0);
                            List<TableFieldInfo> tableFieldInfos = tableFieldInfoMapper.selectList(new QueryWrapper<TableFieldInfo>().eq("table_id", tableInfo1.getId()).orderByAsc("field_order", "is_partition"));
                            if (CollectionUtil.isNotEmpty(tableFieldInfos)) {
                                //判断对应的表的字段是否满足权限
                                List<TableFieldInfo> allList = new ArrayList<>();
                                for (TableFieldInfo tableFieldInfo : tableFieldInfos) {
                                    if (checkedConlum(allConlumIds, tableFieldInfo, allPermissionConfigCulonm)) {
                                        allList.add(tableFieldInfo);
                                    }
                                }
                                for (TableFieldInfo tableFieldInfo : allList) {
                                    JSONObject fieldObj = new JSONObject();
                                    fieldObj.put("label", tableFieldInfo.getFieldName());
                                    fieldObj.put("fieldType", tableFieldInfo.getFieldType());
                                    fieldObj.put("fieldAlias", tableFieldInfo.getFieldAlias());
                                    fieldObj.put("isPartition", tableFieldInfo.getIsPartition());
                                    fieldArray.add(fieldObj);
                                }
                            }
                        }
                    }
                }
            }
        }
        return array;
    }

    private List<IdAndNameDto> changeDataList(List<TableInfo> tableInfos) {
        if (CollectionUtil.isEmpty(tableInfos)) {
            return null;
        }
        List<IdAndNameDto> resList = new ArrayList<>();
        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        tableInfos.forEach(tableInfo -> {
            IdAndNameDto idAndNameDto = new IdAndNameDto();
            idAndNameDto.setId(tableInfo.getId());
            idAndNameDto.setName(tableInfo.getTableName());
            idAndNameDto.setDbName(tableInfo.getDbName());
            if (null == tableInfo.getTableLevelId()) {
                if (null != allPermissionConfig) {
                    idAndNameDto.setTableLevelId(allPermissionConfig.getDataPermissionId());
                }
            } else {
                idAndNameDto.setTableLevelId(tableInfo.getTableLevelId());
            }
            if (null != idAndNameDto.getTableLevelId()) {
                resList.add(idAndNameDto);
            }
        });
        return resList;
    }

    private void pingChildrenData(TableInfo tableInfo, JSONArray tableArray, JSONArray fieldArray) {
        JSONObject tableObj = new JSONObject();
        tableObj.put("dbName", tableInfo.getDbName());
        tableObj.put("label", tableInfo.getTableName());
        tableObj.put("tableAlias", tableInfo.getTableAlias());
        tableObj.put("desc", tableInfo.getDesc());
        tableObj.put("format", tableInfo.getFormat());
        tableArray.add(tableObj);
        tableObj.put("children", fieldArray);
    }

    /**
     * @Description 挑选出表中的对应的可以查看的字段
     * @Author hujz
     * @Date 2019/12/19 13:46
     * @Param allConlumIds：所有的表字段级别   tableIds: 所有的表
     * @Return
     * @Exception
     */
    private List<TableAndConlumDto> pickAllRoleConlum(Set<Integer> allConlumIds, List<IdAndNameDto> tableIds) {
        if (CollectionUtil.isEmpty(tableIds) || CollectionUtil.isEmpty(allConlumIds)) {
            return null;
        }
        //判断对应的字段是否有权限查看
        List<TableAndConlumDto> tableAndConlumDtos = new ArrayList<>();
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        for (IdAndNameDto idAndNameDto : tableIds) {
            TableAndConlumDto tableAndConlumDto = new TableAndConlumDto();
            tableAndConlumDto.setTableId(idAndNameDto.getId());
            tableAndConlumDto.setDbName(idAndNameDto.getDbName());
            tableAndConlumDto.setTableName(idAndNameDto.getName());
            List<TableFieldInfo> tableFieldInfos = tableFieldInfoMapper.selectList(new QueryWrapper<TableFieldInfo>().eq("table_id", idAndNameDto.getId()).orderByAsc("field_order", "id"));
            if (CollectionUtil.isEmpty(tableFieldInfos)) {
                continue;
            }
            //判断对应的表的字段是否满足权限
            List<String> allList = new ArrayList<>();
            for (TableFieldInfo tableFieldInfo : tableFieldInfos) {
                if (checkedConlum(allConlumIds, tableFieldInfo, allPermissionConfigCulonm)) {
                    allList.add(tableFieldInfo.getFieldName());
                }
            }
            if (CollectionUtil.isNotEmpty(allList)) {
                tableAndConlumDto.setConlumList(allList);
                tableAndConlumDtos.add(tableAndConlumDto);
            }
        }
        return tableAndConlumDtos;
    }

    /**
     * @Description 字段是否可以查看
     * @Author hujz
     * @Date 2019/12/19 17:42
     * @Param allConlumIds：用户可以看到的所有的字段水平  tableFieldInfo：字段的级别
     * @Return true:满足字段权限，可以看到   false:不满足，不能看到这个字段
     * @Exception
     */
    private boolean checkedConlum(Set<Integer> allConlumIds, TableFieldInfo tableFieldInfo, AllPermissionConfig allPermissionConfigCulonm) {
        if (CollectionUtil.isEmpty(allConlumIds)) {
            return false;
        }
        if (null == tableFieldInfo.getColcumLevelId()) {
            if (null == allPermissionConfigCulonm) {
                return false;
            }
            if (allConlumIds.contains(allPermissionConfigCulonm.getDataPermissionId())) {
                return true;
            }
        } else {
            if (allConlumIds.contains(tableFieldInfo.getColcumLevelId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description 验证角色对应的表权限
     * @Author hujz
     * @Date 2019/12/19 13:46
     * @Param allDbIds：所有的数据库id   tableIds: 所有的表
     * @Return
     * @Exception
     */
    private void cheackAllRoleTable(Set<Integer> alltableIds, List<IdAndNameDto> tableIds) {
        if (CollectionUtil.isEmpty(tableIds)) {
            return;
        }
        if (CollectionUtil.isEmpty(alltableIds)) {
            throw new RRException(String.format(DataDevelopmentBizExceptionEnum.ROLE_NO_PREMISSION_TABLE.getMessage(), tableIds.get(0).getName()));
        }
        tableIds.forEach(idAndNameDto -> {
            if (!alltableIds.contains(idAndNameDto.getTableLevelId())) {
                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.ROLE_NO_PREMISSION_TABLE.getMessage(), tableIds.get(0).getName()));
            }
        });
    }

    /**
     * @Description 验证角色对应的库权限
     * @Author hujz
     * @Date 2019/12/19 13:46
     * @Param allDbIds：所有的数据库id   tableIds: 所有的表
     * @Return
     * @Exception
     */
    private void cheackAllRoleDb(Set<Integer> allDbIds, List<IdAndNameDto> tableIds) {
        if (CollectionUtil.isEmpty(tableIds)) {
            return;
        }
        if (CollectionUtil.isEmpty(allDbIds)) {
            throw new RRException(String.format(DataDevelopmentBizExceptionEnum.USER_NO_PREMISSION_DB.getMessage(), tableIds.get(0).getDbName()));
        }
        List<DataPermissionParam> allDataPermissionParamList = dataPermissionParamMapper.selectListByTypeAndIds(allDbIds, PressionParamType.DB.getId());
        if (CollectionUtil.isEmpty(allDataPermissionParamList)) {
            throw new RRException(String.format(DataDevelopmentBizExceptionEnum.USER_NO_PREMISSION_DB.getMessage(), tableIds.get(0).getDbName()));
        }
        Set<String> allDbSet = new HashSet<>();
        for (DataPermissionParam dataPermissionParam : allDataPermissionParamList) {
            allDbSet.add(dataPermissionParam.getPermissName());
        }
        //查询到所有的db的名字
        tableIds.forEach(idAndNameDto -> {
            if (!allDbSet.contains(idAndNameDto.getDbName().toUpperCase())) {
                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.USER_NO_PREMISSION_DB.getMessage(), idAndNameDto.getDbName()));
            }
        });
    }

    private Set<Integer> changeData(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return null;
        }
        String[] splitIds = ids.split(",");
        if (null == splitIds || splitIds.length <= 0) {
            return null;
        }
        Set<Integer> set = new HashSet<>();
        for (String splitId : splitIds) {
            try {
                set.add(Integer.valueOf(splitId));
            } catch (Exception e) {
                log.error("转换角色权限参数数据异常，err:{}", e.getMessage());
            }
        }
        return set;
    }

    /**
     * @Description 验证是否是自己创建的表或者是自己加入的项目的表
     * @Author hujz
     * @Date 2019/12/19 13:46
     * @Param userId：用户id   tableIds: 所有的表
     * @Return
     * @Exception
     */
    private void cheackIsMyTableOrAddProTable(Long userId, List<IdAndNameDto> tableIds) {

        if (null == userId || CollectionUtil.isEmpty(tableIds)) {
            return;
        }
        Set<Integer> allTableIds = new HashSet<>();
        //先查询自己的表是否有不存在的。
        List<TableInfo> myTableList = tableInfoMapper.selectList(new QueryWrapper<TableInfo>().eq("create_per", userId));
        if (CollectionUtil.isNotEmpty(myTableList)) {
            myTableList.forEach(tableInfo -> {
                allTableIds.add(tableInfo.getId());
            });
        }
        //再查询加入的项目是否，关联度表是否存在
        List<Integer> proAllTableIds = procUserMapper.selectByUserId(userId);
        if (CollectionUtil.isNotEmpty(proAllTableIds)) {
            allTableIds.addAll(proAllTableIds);
        }
        if (CollectionUtil.isEmpty(allTableIds)) {
            throw new RRException(String.format(DataDevelopmentBizExceptionEnum.TABLE_NO_PREMISSION.getMessage(), ""));
        }
        tableIds.forEach(idAndNameDto -> {
            if (!allTableIds.contains(idAndNameDto.getId())) {
                throw new RRException(String.format(DataDevelopmentBizExceptionEnum.TABLE_NO_PREMISSION.getMessage(), idAndNameDto.getName()));
            }
        });
    }

    /**
     * 我有权限的列表查询 (实时表)
     *
     * @param realApproveQueryDTO
     * @return
     */
    @Override
    public IPage<RealApproveReturnDTO> realList(RealApproveQueryDTO realApproveQueryDTO) {
        Map<String, Object> params = new HashMap<>();
        Integer userId = ShiroUtils.getUserId().intValue();
        params.put("create_per", userId);
        params.put("appUser", userId);
        params.put("sidx", "id");
        params.put("order", "desc");
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.REAL_TABLE.getType(), PermissionTypeEnum.QUERY.getType());
        IPage<RealApproveReturnDTO> page = new Page<>(realApproveQueryDTO.getCurrent(), realApproveQueryDTO.getSize());
        page = this.baseMapper.realList(page, params, realApproveQueryDTO);
        if (CollectionUtil.isEmpty(page.getRecords())) {
            return page;
        }
        Map<Integer, String> projectNamesMap = Maps.newHashMap();
        List<Integer> ids = page.getRecords().stream().map(RealApproveReturnDTO::getId).collect(Collectors.toList());
        List<TableInfo> projectNames = baseMapper.getTableProjectNamesByTableId(ids);
        if (CollectionUtil.isNotEmpty(projectNames)) {
            for (TableInfo projectName : projectNames) {
                projectNamesMap.put(projectName.getId(), projectName.getProjectName());
            }
        }
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        for (RealApproveReturnDTO realApproveReturnDTO : page.getRecords()) {
            //添加创建用户的信息
            Long user = Long.valueOf(realApproveReturnDTO.getCreateUser());
            realApproveReturnDTO.setCreatePerName(userMap.get(user));
            realApproveReturnDTO.setProjectName(projectNamesMap.get(realApproveReturnDTO.getId()));
        }
        return page;
    }

    /**
     * 清空
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delRealTable(Integer id) {
        bdpRealTableInfoMapper.delById(id);
        realTableFieldMapper.delByTableId(id);
    }


    /**
     * @param userId
     * @param tableType
     * @return
     */
    @Override
    public List<JSONObject> applyUserList(Integer userId, Integer tableType) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<Integer> userIds = bdpRealTableInfoMapper.applyUserList(userId, tableType);
        if (CollectionUtils.isEmpty(userIds)) {
            return jsonObjects;
        }
        List<SysUserEntity> list = sysUserService.list(new LambdaQueryWrapper<SysUserEntity>().in(SysUserEntity::getUserId, userIds));
        if (CollectionUtils.isEmpty(list)) {
            return jsonObjects;
        }
        for (SysUserEntity sysUserEntity : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", sysUserEntity.getUserId());
            jsonObject.put("userName", sysUserEntity.getUsername());
            jsonObjects.add(jsonObject);
        }
        return jsonObjects;

    }


    @Override
    public List<JSONObject> createUserList(List<Integer> statusList, Map<String, Object> params) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<Integer> userIds = bdpRealTableInfoMapper.createUserList(statusList, params);
        if (CollectionUtils.isEmpty(userIds)) {
            return jsonObjects;
        }
        List<SysUserEntity> list = sysUserService.list(new LambdaQueryWrapper<SysUserEntity>().in(SysUserEntity::getUserId, userIds));
        if (CollectionUtils.isEmpty(list)) {
            return jsonObjects;
        }
        for (SysUserEntity sysUserEntity : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId", sysUserEntity.getUserId());
            jsonObject.put("userName", sysUserEntity.getUsername());
            jsonObjects.add(jsonObject);
        }
        return jsonObjects;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        //tableInfoService.setTenantParams(params,PermissionTypeEnum.STORE.getType(),PermissionTypeEnum.QUERY.getType(),ShiroUtils.getTenantId());
        //params.put("currentTenantId",ShiroUtils.getTenantId());
        IPage<TableInfo> page = this.baseMapper.listNoRelation((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        Map<Integer, String> projectNamesMap = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            List<Integer> ids = page.getRecords().stream().map(TableInfo::getId).collect(Collectors.toList());
            List<TableInfo> projectNames = baseMapper.getTableProjectNamesByTableId(ids);
            if (CollectionUtil.isNotEmpty(projectNames)) {
                for (TableInfo projectName : projectNames) {
                    projectNamesMap.put(projectName.getId(), projectName.getProjectName());
                }
            }
        }
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        for (TableInfo tableInfo : page.getRecords()) {
            //添加创建用户的信息
            Long userId = Long.valueOf(tableInfo.getCreatePer());
            tableInfo.setCreatePerName(userMap.get(userId));
            tableInfo.setProjectName(projectNamesMap.get(tableInfo.getId()));
        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryUserTablePage(Map<String, Object> params) {
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        IPage<TableInfo> page = this.baseMapper.usertableListNoRelation((Page<TableInfo>) new Query<TableInfo>().getPage(params), params);
        Map<Integer, String> projectNamesMap = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            List<Integer> ids = page.getRecords().stream().map(TableInfo::getId).collect(Collectors.toList());
            List<TableInfo> projectNames = baseMapper.getTableProjectNamesByTableId(ids);
            if (CollectionUtil.isNotEmpty(projectNames)) {
                for (TableInfo projectName : projectNames) {
                    projectNamesMap.put(projectName.getId(), projectName.getProjectName());
                }
            }
        }
        List<SysUserDto> userList = sysUserService.listAllUsers();
        Map<Long, String> userMap = userList.stream().collect(
                Collectors.toMap(SysUserDto::getUserId, SysUserDto::getUsername));
        for (TableInfo tableInfo : page.getRecords()) {
            //添加创建用户的信息
            Long userId = Long.valueOf(tableInfo.getCreatePer());
            tableInfo.setCreatePerName(userMap.get(userId));
            tableInfo.setProjectName(projectNamesMap.get(tableInfo.getId()));
        }
        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> queryUserList(Map<String, Object> params) {
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        return this.baseMapper.userList(params);
    }

    @Override
    public List<TableInfo> selectTableByProc(String dbName, String tableName) {
        Map<String, Object> params = new HashMap<>();
        params.put("dbName", dbName);
        if (StringUtils.isNotEmpty(tableName)) {
            params.put("tableName", tableName);
        }
        DataPermissionUtil.putDataFilter(params,
                PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
        return this.baseMapper.selectTableByProc(params);
    }

    @Override
    public List<TableInfo> selectUserProcTables(String dbName, Integer tableId) {
        return baseMapper.selectUserProcTables(dbName, ShiroUtils.getUserId().intValue(), tableId);
    }

    @Override
    public List<TableInfo> selectUserNotProcTables(String dbName, Integer tableId) {
        return baseMapper.selectUserNotProcTables(dbName, ShiroUtils.getUserId().intValue(), tableId);
    }

    @Override
    public List<TableInfo> selectTableByProcWithNoProc(String dbName, Integer tableId) {
        List<TableInfo> resultList = new ArrayList<>();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("db_name", dbName);
            if (tableId != null) {
                params.put("id", tableId);
            }
            DataPermissionUtil.putDataFilter(params,
                    PermissionTypeEnum.STORE.getType(), PermissionTypeEnum.QUERY.getType());
            resultList = baseMapper.selectListPage(params);
           /* List<TableInfo> userProcTables = this.selectUserProcTables(dbName,tableId);
            if (CollectionUtils.isNotEmpty(userProcTables)) {
                resultList.addAll(userProcTables);
            }
            List<TableInfo> userNotProcTables = this.selectUserNotProcTables(dbName, tableId);
            if (CollectionUtils.isNotEmpty(userNotProcTables)) {
                resultList.addAll(userNotProcTables);
            }*/
        } catch (Exception e) {
            log.error("获取用户参与的项目的表以及用户创建未关联项目的表出错：{}", e.getMessage());
        }
        return resultList;
    }

    @Override
    public List<TableInfo> selectExitPartitionFieldTables() {
        return tableInfoMapper.selectExitPartitionFieldTables();
    }

    @Override
    public TableInfo getTableById(Integer tableId) {
        TableInfo table = tableInfoMapper.getTableById(tableId);
        table.setTableFieldInfos(tableFieldInfoMapper.selectListByTableIds(Collections.singleton(tableId)));
        return table;
    }

    @Override
    public List<RoleRowAndRoleDataPermissionDto> selectTableFileRuleIds(Integer tableId) {
        return tableFieldInfoMapper.selectTableFileRuleIds(tableId);
    }

}
